Skip to content

Instantly share code, notes, and snippets.

@blaix
Created March 31, 2010 18:51
Show Gist options
  • Save blaix/350714 to your computer and use it in GitHub Desktop.
Save blaix/350714 to your computer and use it in GitHub Desktop.
Showing off some niceties of ruby.
class Person
attr_reader :name
def initialize(name, height)
@name = name
@height = height
end
def tall?
@height == "tall"
end
end
class Population
def <<(person)
@people ||= []
@people << person
end
def find
@people.each do |person|
return person if yield(person)
end
end
end
my_city = Population.new
my_city << Person.new("justin", "tall")
my_city << Person.new("thomas", "short")
tall_person = my_city.find { |person| person.tall? }
thomas = my_city.find { |person| person.name == "thomas" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment