You are on page 1of 2

class RecursiveList

def initialize(element)
@element = element
end
def add(element)
if last?
@tail = RecursiveList.new(element)
else
@tail.add(element)
end
end
def last?
@tail.nil?
end
def element
@element
end
def tail
@tail
end
# Bigger implementation
def bigger(&block)
biggerElem(element, &block)
end
def big(&block)
if last?
return element
else
return yield element, tail.big(&block)
end
end
def biggerElem(elem, &block)
if last?
return elem
end
return yield(element, tail.bigger(&block))
end
end
list1 = RecursiveList.new(10)
list1.add(20)
list1.add(50)
list1.add(90)
list1.add(30)
list2 = RecursiveList.new("short")
list2.add("super longer")
list2.add("longer")
list2.add("super long")
puts list1.bigger { |x , y | (x > y )? x : y }

puts list2.bigger { |x , y | (x.length > y.length )? x : y }


list1.big { |x , y | (x > y )? x : y }

You might also like