Skip to content

Instantly share code, notes, and snippets.

@delba
Last active January 2, 2016 23:19
Show Gist options
  • Save delba/8375287 to your computer and use it in GitHub Desktop.
Save delba/8375287 to your computer and use it in GitHub Desktop.
Ruby finders
class Person
@all = []
def self.all
@all
end
def self.where(conditions)
all.select do |person|
conditions.map do |k, v|
v = "'#{v}'" if v.is_a? String
person.instance_eval [k, v].join
end.all?
end
end
attr_reader :name, :age
def initialize(name, age)
@name, @age = name, age
Person.all << self
end
end
marc = Person.new('marc', 23)
sophie = Person.new('sophie', 24)
Person.where('name==' => 'marc', 'age<=' => 23) # => [marc]
Person.where('age<=' => 24) # => [marc, sophie]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment