Difference between revisions of "Learn Ruby Programming"
Jump to navigation
Jump to search
| Line 28: | Line 28: | ||
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 94: | Line 104: | ||
irb(main):069:0> 36.class | irb(main):069:0> 36.class | ||
=> Fixnum | => Fixnum | ||
| + | |||
| + | "Hello".class | ||
| + | |||
</pre> | </pre> | ||
| Line 108: | Line 121: | ||
</pre> | </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> | ||
| + | && | ||
| + | </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 | ||
| + | 3.25=>nil | ||
| + | </pre> | ||
| + | |||
| + | * Constant | ||
| + | <pre> | ||
| + | Pi = 3.14 | ||
| + | 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> | ||
[[Category:Learn_Ruby]] | [[Category:Learn_Ruby]] | ||
Revision as of 09:53, 23 February 2012
- Need IRB (Interactive Ruby)
- 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
&&
- 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 3.25=>nil
- Constant
Pi = 3.14 print Pi 3.14=> nil Pi = 3 warning: already initialized constant Pi print Pi 3=> nil # Just a warning, the value is actually changed.