Learn Ruby Programming


 * Need IRB (Interactive Ruby)
 * http://rubyinstaller.org/
 * http://rubyonrails.org/download

irb(main):022:0> 2 + 4 => 6
 * Simple commands

irb(main):023:0> print "Hello World" Hello World=> nil

irb(main):024:0> 3.times do print "Yeah" end YeahYeahYeah=> 3

irb(main):027:0> 7/3 => 2

irb(main):028:0> 7.0/3 => 2.3333333333333335

irb(main):025:0> x = 12 => 12
 * Variable

irb(main):026:0> x + 13 => 25

Variable is case-sensitive. M = 29

M


 * Shortcuts

g += 1 M *= 4

irb(main):029:0> class Vehicle irb(main):030:1> attr_accessor :name, :type, :make, :model irb(main):031:1> end => nil
 * Object Oriented - Class

irb(main):039:0> my_vehicle = Vehicle.new => #
 * Object Oriented - Instantiation - Creating an Object

irb(main):040:0> my_vehicle.name = "Work Car" => "Work Car"

irb(main):041:0> my_vehicle.make = "Honda" => "Honda"

irb(main):045:0> puts my_vehicle.name + " is " + my_vehicle.make Work Car is Honda => nil

irb(main):051:0* class Bus < Vehicle irb(main):052:1> attr_accessor :num_seats irb(main):053:1> end => nil
 * Object Oriented - Inheritance

irb(main):054:0> my_bus = Bus.new => #

irb(main):055:0> my_bus.make = "GMC" => "GMC"

irb(main):056:0> my_bus.num_seats = 30 => 30

irb(main):092:0> class Bus < Vehicle irb(main):093:1> def show_make irb(main):094:2> puts "Make: " + make irb(main):095:2> end irb(main):096:1> end => nil
 * Object Oriented - Method

irb(main):097:0> my_bus.show_make Make: GMC => nil

irb(main):068:0* my_bus.class => Bus
 * Object Oriented - What class does an object come from?

irb(main):069:0> 36.class => Fixnum

"Hello".class

irb(main):074:0> puts "Hello" Hello => nil
 * Object Oriented - Kernel Methods - No need to prefix it with Kernel.

irb(main):075:0> Kernel.puts "Hello" Hello => nil

puts("hello") same as puts "hello"
 * Arguments

"Hello".length "Hello".reverse "Hello".upcase "Hello".reverse.upcase # concatenation
 * String

def display_hello puts "Hello" end
 * Function or Method without Class

display_hello

puts "hello" if M < 20
 * Conditions or decision making

== < > <= >= !=
 * Comparison operators

&& AND
 * Boolean operators
 * OR

8.times do puts "Hello" end
 * Loop

8.times {puts "Hello"}

3.upto(7) {puts "Hello"} # 5 loops

14.downto(9) {puts "Hello"} # 6 loops

1.step(9,2) {puts "Hello"} # 5 loops

1.upto(10) {|c| puts c}
 * Extract the loop count number

> c = 13 > print c.to_f/4 # to_f to convert to floating 3.25=>nil
 * Data type conversion

> Pi = 3.14   # Initcap the variable > print Pi 3.14=> nil
 * Constant

> Pi = 3 warning: already initialized constant Pi

> print Pi 3=> nil  # Just a warning, the value is actually changed.

> message = < "Hello\nWorld!\nYes\n"
 * Multi-line string variable

> print message Hello World! Yes => nil

> a = 4 > b = 6 > puts "#{a} * #{b} = #{a*b}" 4 * 6 = 24 => nil
 * Interpolation

> text = "World" > puts "Hello #{text}" Hello World => nil

> puts "Hello World".sub("World","Ocean") Hello Ocean
 * Substitution / Regular Expression

> puts "Hello World World".sub("World","Ocean") # Only the first instance Hello Ocean World

> puts "Hello World World".gsub("World","Ocean") # Use global sub Hello Ocean Ocean

> puts "Hello World".sub(/H[a-z]*/,"Hi") Hi World

> a = "Hello World" > a.scan(/.../) {|x| puts x} Hel lo Wor

> x = "Hello 300 world 2012" > x.scan(/\d+/) {|z| puts z}  # Extract numbers 300 2012

> "Hello World".scan(/[ol]/) {|x| puts x}  # Character class l l o o l

> "Hello World".scan(/[a-f]/) {|x| puts x} # Range of characters e d

> puts "Found number" if "Hello World2" =~ /[0-9]/ Found number

> puts "Found character" if "662-892-55g5".match(/[a-z|A-Z]/) Found character