# Arrays x = Array.new() x.push(5) x.push(Array.new()) x.push(7) x.pop() #returns 7 y = x y.pop() #returns [] x.pop() #returns 5 -- x and y are aliased x.push(6).push(7) y = x.clone() y.pop() # returns 7 x.pop() # returns 7 also x = Array.new(3) # returns [nil, nil, nil] x = [5, 6, 7] x = Array.new(3) {|n| n + 5} x = Array.new(3, 6) # returns [6, 6, 6] puts x[1] #6 x[1] = 666 puts x[1] #666 puts x[20] #nil puts x[-2] #666 x.delete(6) # deletes all values 6 from the array x # is now [] # Hashes x = Hash.new() x["a"] = "Found A" x[false] = "Found false" puts x["a"] puts x[false] puts x.keys puts x.values x = Hash.new("NOT FOUND") x["a"] = "Found A" x["b"] = "Found B too" puts x["a"] puts x["c"] x.delete("a") # returns "Found A" and deletes it from the hash # Class variables and methods class Foo @@counter = 0 # must initialize def initialize @@counter++ end def Foo.report1 @@counter end def self.report2 # same thing as Foo.report1 @@counter end end x = Foo.new y = Foo.new numFoos = Foo.report1 # returns 2 -- note it doesn't require a Foo object to run