Learn Ruby Programming

From Ittichai Chammavanijakul's Wiki
Jump to navigation Jump to search
  • Simple commands
irb(main):022:0> 2 + 4
=> 6

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
  • Variable
irb(main):025:0> x = 12
=> 12

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

Variable is case-sensitive.
M = 29

M

* Shortcuts

g += 1
M *= 4

  • Object Oriented - Class
irb(main):029:0> class Vehicle
irb(main):030:1> attr_accessor :name, :type, :make, :model
irb(main):031:1> end
=> nil
  • Object Oriented - Instantiation - Creating an Object
irb(main):039:0> my_vehicle = Vehicle.new
=> #<Vehicle:0x29583c0>

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

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

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

irb(main):056:0> my_bus.num_seats = 30
=> 30
  • Object Oriented - Method
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

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

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

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

"Hello".class

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

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


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

* String
"Hello".length
"Hello".reverse
"Hello".upcase
"Hello".reverse.upcase  # concatenation
  • Function or Method without Class
def display_hello
puts "Hello"
end

display_hello
  • Conditions or decision making
puts "hello" if M < 20
  • Comparison operators
==
< > <= >=
!=
  • Boolean operators
&&  AND
||  OR
  • Loop
8.times do puts "Hello" end

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
  • Extract the loop count number
1.upto(10) {|c| puts c}
  • Data type conversion
> c = 13
> print c.to_f/4  # to_f to convert to floating
3.25=>nil
  • Constant
> Pi = 3.14    # Initcap the variable
> print Pi
3.14=> nil

> Pi = 3
warning: already initialized constant Pi

> print Pi
3=> nil   # Just a warning, the value is actually changed.
  • Multi-line string variable
> message = <<mark    # mark can be any words
Hello
World!
Yes
mark
=> "Hello\nWorld!\nYes\n"

> print message
Hello
World!
Yes
=> nil
  • Interpolation
> a = 4
> b = 6
> puts "#{a} * #{b} = #{a*b}"
4 * 6 = 24
=> nil

> text = "World"
> puts "Hello #{text}"
Hello World
=> nil
  • Substitution / Regular Expression
> puts "Hello World".sub("World","Ocean")
Hello Ocean

> 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