P. 1
Ruby an Introduction

Ruby an Introduction

|Views: 20|Likes:
Published by Shrini Vasan

More info:

Categories:Types, Speeches
Published by: Shrini Vasan on Oct 15, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as ODP, PDF, TXT or read online from Scribd
See more
See less

10/15/2012

pdf

text

original

Ruby – An Introduction

Tha.Suresh jemenisuresh@gmail.com Indian Linux User Group, Chennai

Ruby is a Programming Language

There are so many 

Programming Languages.

Why 

Ruby?

Ruby is simple and beautiful

Ruby is Easy to Learn

Ruby is Free Open Source Software

Ruby on Rails – Web Framework

Rubyforge

9112 projects and libraries http://rubyforge.org/

RAA
● ●

Ruby Application Archive 1830 projects

http://raa.ruby-lang.org

Can do

Text Handling System Administration GUI programming Web Applications Database Apps Scientific Applications

Games NLP ...

History

Ruby
Yukihiro “Matz” Matsumoto Japan February 24, 1993

 Perl  Java  Python  Ruby  PHP 1987       1991         1993  1995

        What is Ruby?

Ruby is… A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.

Quick and Easy

     Intrepreted Scripting Language      Variable declarations are unnecessary      Variables are not typed      syntax is simple and consistent      memory management is automatic

Object Oriented Programming

     Everything is an object      classes, methods, inheritance, etc.      singleton methods      "mixin" functionality by module      iterators and closures

Examples!

5.times { print “Ruby! " }

5.times { print “Ruby! " } Ruby! Ruby! Ruby! Ruby! Ruby!

Everything is an object

100.next

100.next 101

“I love Ruby”  .reverse.capitalize

“I love Ruby”  .reverse.capitalize “Ybur evol i”

3.hours.from_now

3.hours.from_now Thu Jan 31 22:05:00 Eastern Standard Time 2011

Conventions

Variables

colored_index_cards

Class Names

Person

Symbols

:street_name

Instance Variables

@school_name

Constants

Kilograms_Per_Pound

Input

puts “What is Your name?” name = gets name = name.chomp puts "Hello" + name + " .Welcome"

            Flow

if ( score >= 5000 ) puts “You win!” elsif ( score <= 0 ) puts “Game over.” else puts “Current score: #{score}” end

puts “PASS” if mark > 35

                    Loop

count = 0 5.times do count += 1 puts "Count =" + count.to_s end

Count Count Count Count Count

= = = = =

1 2 3 4 5

count = 0 while count < 10 puts "Count = " +count.to_s count += 1 end

Blocks

1.upto(5) { |x| puts x } 1 2 3 4 5

5.downto(1) do |time| print “#{time}... ” puts “!” if time <= 3 end 5... 4... 3... ! 2... ! 1... !

Array

Array
numbers = [ "zero", "one", "two", "three", "four" ]  

Array
numbers = [ "zero", "one", "two", "three", "four" ]   >> numbers[0] => "zero" >> numbers[4] => "four"

Array
numbers = [ "zero", "one", "two", "three", "four" ] >> numbers[3].upcase => "THREE" >> numbers[3].reverse => "eerht"

Sort Array
primes = [ 11, 5, 7, 2, 13, 3 ]

Sort Array
primes = [ 11, 5, 7, 2, 13, 3 ] primes.sort

Sort Array
primes = [ 11, 5, 7, 2, 13, 3 ] primes.sort => [2, 3, 5, 7, 11, 13]

Sort Array
names = [ "Shrini", "Bala", "Suresh", "Arul"]

Sort Array
names = [ "Shrini", "Bala", "Suresh", "Arul"] names.sort

Sort Array
names = [ "Shrini", "Bala", "Suresh", "Arul"] names.sort =>["Arul", "Bala", "Shrini", "Suresh"]

More on Arrays

Reverse Length Delete Join Find More than 100 methods

Hashes

menu = { :idly :dosai :coffee :ice_cream } menu[:idly] 2.50

=> => => =>

2.50, 10.00, 5.00, 5.00

Methods

Methods
def say_hello(name) result = “Hello, #{name}!” return result end puts say_hello(“world”)

Methods
def say_hello(name) “Hello, #{name}!” end puts say_hello(“world”)

Class

Classes
class MathWhiz def say_square(value) puts value * value end end sam = MathWhiz.new sam.say_square(5)

Inheritance
class Dog < Animal @catagory = “mammal” @legs = 4 end

Module

Modules
module Trig PI = 3.141592654 def Trig.sin(x) # .. end def Trig.cos(x) # .. end end

Modules
require "trig" y = Trig.sin(Trig::PI/4) 0.707106780551956

Attributes
class PlainOldRubyObject attr_accessor :food, :drinks attr_reader :advice attr_writer :write_only end

Scope
class Poet #public by default def poetry end protected def family_legacy end private def hopes_and_dreams end end

THE END
of code :-)

How to Learn?

irb
● ● ● ●

Interactive ruby A ruby Shell Instance response learn as you type

Web sites

Web Sites

http://ruby­lang.org

www.ruby­lang.org

www.rubyforge.net

http://www.ruby­forum.com/

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->