Difference between revisions of "Learn Ruby Programming"

From Ittichai Chammavanijakul's Wiki
Jump to navigation Jump to search
(Created page with "* Need IRB (Interactive Ruby) http://rubyinstaller.org/ http://rubyonrails.org/download * Simple <pre> irb(main):022:0> 2 + 4 => 6 irb(main):023:0> print "Hello World" Hello Wo...")
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
* Need IRB (Interactive Ruby)
 
* Need IRB (Interactive Ruby)
http://rubyinstaller.org/
+
** http://rubyinstaller.org/
http://rubyonrails.org/download
+
** http://rubyonrails.org/download
  
* Simple
+
* Simple commands
 
<pre>
 
<pre>
 
irb(main):022:0> 2 + 4
 
irb(main):022:0> 2 + 4
Line 16: Line 16:
 
irb(main):027:0> 7/3
 
irb(main):027:0> 7/3
 
=> 2
 
=> 2
 +
 
irb(main):028:0> 7.0/3
 
irb(main):028:0> 7.0/3
 
=> 2.3333333333333335
 
=> 2.3333333333333335
Line 24: Line 25:
 
irb(main):025:0> x = 12
 
irb(main):025:0> x = 12
 
=> 12
 
=> 12
 +
 
irb(main):026:0> x + 13
 
irb(main):026:0> x + 13
 
=> 25
 
=> 25
 +
 +
Variable is case-sensitive.
 +
M = 29
 +
 +
M
 +
 +
* Shortcuts
 +
 +
g += 1
 +
M *= 4
  
 
</pre>
 
</pre>
Line 41: Line 53:
 
irb(main):039:0> my_vehicle = Vehicle.new
 
irb(main):039:0> my_vehicle = Vehicle.new
 
=> #<Vehicle:0x29583c0>
 
=> #<Vehicle:0x29583c0>
 +
 
irb(main):040:0> my_vehicle.name = "Work Car"
 
irb(main):040:0> my_vehicle.name = "Work Car"
 
=> "Work Car"
 
=> "Work Car"
 +
 
irb(main):041:0> my_vehicle.make = "Honda"
 
irb(main):041:0> my_vehicle.make = "Honda"
 
=> "Honda"
 
=> "Honda"
 +
 
irb(main):045:0> puts my_vehicle.name + " is " + my_vehicle.make
 
irb(main):045:0> puts my_vehicle.name + " is " + my_vehicle.make
 
Work Car is Honda
 
Work Car is Honda
 
=> nil
 
=> nil
 +
</pre>
  
 
* Object Oriented - Inheritance
 
* Object Oriented - Inheritance
Line 55: Line 71:
 
irb(main):053:1> end
 
irb(main):053:1> end
 
=> nil
 
=> nil
 +
 
irb(main):054:0> my_bus = Bus.new
 
irb(main):054:0> my_bus = Bus.new
 
=> #<Bus:0x282fd50>
 
=> #<Bus:0x282fd50>
 +
 
irb(main):055:0> my_bus.make = "GMC"
 
irb(main):055:0> my_bus.make = "GMC"
 
=> "GMC"
 
=> "GMC"
 +
 
irb(main):056:0> my_bus.num_seats = 30
 
irb(main):056:0> my_bus.num_seats = 30
 
=> 30
 
=> 30
 
</pre>
 
</pre>
 +
 +
* Object Oriented - Method
 +
<pre>
 +
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
 +
 +
</pre>
 +
 +
* Object Oriented - What class does an object come from?
 +
<pre>
 +
irb(main):068:0* my_bus.class
 +
=> Bus
 +
 +
irb(main):069:0> 36.class
 +
=> Fixnum
 +
 +
"Hello".class
 +
 +
</pre>
 +
 +
* Object Oriented - Kernel Methods - No need to prefix it with Kernel.
 +
<pre>
 +
irb(main):074:0> puts "Hello"
 +
Hello
 +
=> nil
 +
 +
irb(main):075:0> Kernel.puts "Hello"
 +
Hello
 +
=> nil
 +
 +
</pre>
 +
 +
 +
* Arguments
 +
<pre>
 +
puts("hello") same as puts "hello"
 +
 +
* String
 +
"Hello".length
 +
"Hello".reverse
 +
"Hello".upcase
 +
"Hello".reverse.upcase  # concatenation
 +
</pre>
 +
 +
* Function or Method without Class
 +
<pre>
 +
def display_hello
 +
puts "Hello"
 +
end
 +
 +
display_hello
 +
</pre>
 +
 +
* Conditions or decision making
 +
<pre>
 +
puts "hello" if M < 20
 +
</pre>
 +
 +
* Comparison operators
 +
<pre>
 +
==
 +
< > <= >=
 +
!=
 +
</pre>
 +
 +
* Boolean operators
 +
<pre>
 +
&&  AND
 +
||  OR
 +
</pre>
 +
 +
* Loop
 +
<pre>
 +
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
 +
</pre>
 +
 +
* Extract the loop count number
 +
<pre>
 +
1.upto(10) {|c| puts c}
 +
</pre>
 +
 +
* Data type conversion
 +
<pre>
 +
> c = 13
 +
> print c.to_f/4  # to_f to convert to floating
 +
3.25=>nil
 +
</pre>
 +
 +
* Constant
 +
<pre>
 +
> 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.
 +
</pre>
 +
 +
* Multi-line string variable
 +
<pre>
 +
> message = <<mark    # mark can be any words
 +
Hello
 +
World!
 +
Yes
 +
mark
 +
=> "Hello\nWorld!\nYes\n"
 +
 +
> print message
 +
Hello
 +
World!
 +
Yes
 +
=> nil
 +
</pre>
 +
 +
* Interpolation
 +
<pre>
 +
> a = 4
 +
> b = 6
 +
> puts "#{a} * #{b} = #{a*b}"
 +
4 * 6 = 24
 +
=> nil
 +
 +
> text = "World"
 +
> puts "Hello #{text}"
 +
Hello World
 +
=> nil
 +
</pre>
 +
 +
* Substitution / Regular Expression
 +
<pre>
 +
> 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
 +
 +
</pre>
 +
 +
[[Category:Learn_Ruby]]

Latest revision as of 12:22, 23 February 2012

  • 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