Last active
January 2, 2016 23:19
-
-
Save delba/8375287 to your computer and use it in GitHub Desktop.
Ruby finders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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