Ruby 101


1. Ruby is …

  • a scripting language designed by Yukihiro Matsumoto (Matz).
  • a pure object-oriented programming language
  • an interpreted programming language

2. Ruby features

2.1. Basic statements

  • Begin: Declares code to be called before the program is run.
  • END: Declares code to be called at the end of the program.
    #!/usr/bin/ruby
    
    puts "This is main Ruby Program"
    
    END {
       puts "Terminating Ruby Program"
    }
    BEGIN {
       puts "Initializing Ruby Program"
    }
    
    # Output
    > Initializing Ruby Program
    > This is main Ruby Program
    > Terminating Ruby Program

2.2. Variable & Constant

Ruby provides four types of variables:

  • Local Variables: the variables defined in a method and not available outside the method. Local variables begin with a lowercase letter or _.

  • Instance Variables: available across methods for any particular instance or object. That means that instance variables change from object to object. Instance variables are preceded by @ and followed by the variable name.

  • Class Variables: Class variables are available across different objects. A class variable belongs to the class and is a characteristic of a class. They are preceded by the sign @@ and are followed by the variable name.

  • Global Variables: Class variables are not available across classes. If you want to have a single variable, which is available across classes, you need to define a global variable. The global variables are always preceded by $.

  • Constant: Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally. Constants may not be defined within methods.

2.3. Class

This example displays:

  • how variables and constants are used
  • how class is defined
#!/usr/bin/ruby -w

class Customer
  @@no_customers = 0

  def initialize(id, name)
    @cust_id = id
    @name = name
    @@no_customers += 1
  end

  def display_details()
    puts "Id: #{@cust_id}"
    puts "Name: #{@name}"
    puts "Number of customers: #{@@no_customers}"
  end
end

c1 = Customer.new(0, 'Jack')
c1.display_details

c2 = Customer.new(1, 'Jane')
c2.display_details

3. Data structures

  • Array: created by placing a comma-separated series of object references between the square brackets. A trailing comma is ignored
  • Hash: placing a list of key/value pairs between braces, with either a comma or the sequence => between the key and the value. A trailing comma is ignored
  • Range: represents an interval which is a set of values with a start and an end. Ranges may be constructed using the s..e and s…e literals, or with Range.new.
#!/usr/bin/ruby

# Array
array = ['toby', 10, 3.14]
array.each { |i| puts i }
array.each do |i|
  puts i
end

> toby
> 10
> 3.14

# Hash
hash = colors = { 'red' => 0xf00, 'green' => 0x0f0 }
hash.each do |key, value|
  print key, ' is ', value, "\n"
end

> red is 3840
> green is 240

# Range (inclusive)
(10..15).each do |n| 
   print n, ' ' 
end

> 10 11 12 13 14 15

# Range (exclusive)
(10...15).each do |n| 
   print n, ' ' 
end

> 10 11 12 13 14 

3.1. Here Document

#!/usr/bin/ruby -w

print <<EOF
   This is the first way of creating
   here document ie. multiple line string.
EOF

print <<"EOF";                # same as above
   This is the second way of creating
   here document ie. multiple line string.
EOF

print <<`EOC`                 # execute commands
	echo hi there
	echo lo there
EOC

print <<"foo", <<"bar"  # you can stack them
	I said foo.
foo
	I said bar.
bar

Author: Shane Tsui
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Shane Tsui !

  TOC