# # Hello world of Ruby objects! puts "Hello, World!" # >> Hello, World! puts "Hello, World!".reverse # >> !dlroW ,olleH # #numbers puts 3 + 4 puts 3.14 * 7 puts 1.01 + 1.99 # >> 7 # >> 21.98 # >> 3.0 # #ranges range = 1..5 puts range puts range.class range = 1..5 range.each { |x| puts x * 2 } puts (1..5).collect { |x| x * 2 } # !> (...) interpreted as grouped expression # >> 2 # >> 4 # >> 6 # >> 8 # >> 10 # >> 2 # >> 4 # >> 6 # >> 8 # >> 10 # #regex re = /(\w+) (\w+)/ re =~ "howdy doody" puts "#{$2} #{$1}" # >> doody howdy a = "Matt" b = "Hello, #{a}, how are you today?" puts b # >> Hello, Matt, how are you today? # # Everything is an Object # puts 3.class # >> Fixnum puts 3.times { puts "Hello, World!" } # >> Hello, World! # >> Hello, World! # >> Hello, World! # >> 3 number = 5 number.times {|x| puts x } # >> 0 # >> 1 # >> 2 # >> 3 # >> 4 # # classes and a little duck typing class Vehicle def blare horn.upcase end end v = Vehicle.new puts v.blare class Car < Vehicle def horn "beep!" end end class Truck < Vehicle def horn "honk!" end end c = Car.new puts c.horn t = Truck.new puts t.horn puts c.blare # >> beep! # >> honk! # >> BEEP! # # Accessors class Book attr_accessor :title attr_writer :author attr_reader :author end book = Book.new book.title = "Programming Ruby" book.author = "Dave Thomas, Andy Hunt" puts "\"#{book.title}\" by #{book.author}" # >> "Programming Ruby" by Dave Thomas, Andy Hunt # # Scoping $GV = "Global Variable" $gv = "global variable.... very bad!" class MyObject Constant = "constant" @instance_variable = "instance_var" @@class_variable = "class_var" def method local_variable = "" end end # # Mixins class Object def hello to_s end end a = "abcdefg" puts a.class puts a.hello puts 3.hello # >> String # >> abcdefg # >> 3 # # Super open class Hello def method_missing(method, *args, &block) puts "creating '#{method}' method..." self.class.send(:define_method, method, &eval("proc{puts \"hello #{method}\"}")) send(method) end end hello = Hello.new hello.bob hello.george hello.bob hello2 = Hello.new hello2.bob # >> creating 'bob' method... # >> hello bob # >> creating 'george' method... # >> hello george # >> hello bob # >> hello bob # # Least Astonishment # # # arrays arr = [1,2,3,"four",5] arr.each { |x| puts x } puts arr.join(",") puts arr.reverse.join(",") # >> 1 # >> 2 # >> 3 # >> four # >> 5 # >> 1,2,3,four,5 # >> 5,four,3,2,1 arr = [23,1,56,4,8,5,18,2] puts arr.sort.reverse # >> 56 # >> 23 # >> 18 # >> 8 # >> 5 # >> 4 # >> 2 # >> 1 # # methods def print_fib_to(max) a = b = 1 while a <= max puts a c = a + b a,b = b,c end end print_fib_to 13 # >> 1 # >> 1 # >> 2 # >> 3 # >> 5 # >> 8 # >> 13 # hashes hash = { "a" => "apple", "b" => "banana", "c" => "cherry" } hash.each {|key, value| puts "#{key} => #{value}"} # >> a => apple # >> b => banana # >> c => cherry # # closures / blocks hash = { "a" => "apple", "b" => "banana", "c" => "cherry" } def do_something_with_hash(hash, array, &something) array.each do |key| something.call hash[key] end end do_something_with_hash(hash, ["a","c"]) { |x| puts "#{x} pies" } # >> apple pies # >> cherry pies # # hashes as parameter catch-alls def myMethod(name, params) puts name params.each { |key, value| puts "#{key} => #{value}" } end myMethod "my favorite song", "title" => "Red House", "artist" => "Jimi Hendrix", "album" => "Are You Experienced?", "year" => 1970 # >> my favorite song # >> artist => Jimi Hendrix # >> title => Red House # >> year => 1970 # >> album => Are You Experienced? # # Symbols sym = :may puts sym puts sym.class # >> may # >> Symbol hash = {:apple => "apple", :banana => "banana", :cherry => "cherry"} puts hash[:apple] # >> apple