Difference between revisions of "Learn Ruby Programming"
Jump to navigation
Jump to search
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 | ||
Line 41: | Line 43: | ||
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 | ||
Line 56: | Line 61: | ||
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> | ||
+ | |||
+ | * 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> | </pre> | ||
Line 77: | Line 100: | ||
irb(main):074:0> puts "Hello" | irb(main):074:0> puts "Hello" | ||
Hello | Hello | ||
+ | => nil | ||
− | |||
irb(main):075:0> Kernel.puts "Hello" | irb(main):075:0> Kernel.puts "Hello" | ||
Hello | Hello | ||
=> nil | => nil | ||
+ | |||
</pre> | </pre> | ||
+ | |||
+ | |||
+ | |||
+ | [[Category:Learn_Ruby]] |
Revision as of 12:16, 11 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
- 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
- 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