# Using arrays. class Memofib def initialize puts "Initializing fibonacci class." @memo = [] end def go(i) if i == 0 || i == 1 return 1 end if @memo[i] @memo[i] else @memo[i] = go(i-1) + go(i-2) end end end #but arrays are other things, too! #A simple queue class using an array as a list. class Queue def initialize puts "Initializing queue." @arr = [] end def push(el) @arr.push(el) # Don't return the internal representation! nil end def peek() @arr[@arr.length - 1] end def pop() @arr.pop nil end end # Also, used as tuples. # A point. class Point def initialize(x,y) puts "Initializing point." @x = x @y = y end def +(p) #pattern matching? x,y = p.getXY() Point.new(@x + x, @y + y) end def getXY() [@x,@y] end end #dictionaries! class Dictionaries def initialize @schedule1 = Hash.new @schedule1["Sunday"] = 0 @schedule1["Monday"] = 0 @schedule1["Tuesday"] = 1 @schedule1["Wednesday"] = 0 @schedule1["Thursday"] = 1 @schedule1["Friday"] = 0 @schedule1["Saturday"] = 0 @schedule2 = {"Sunday" => 0, "Monday" => 0, "Tuesday" => 1, "Wednesday" => 0, "Thursday" => 1, "Friday" => 0, "Saturday" => 0} end attr_reader :schedule1, :schedule2 end