Created
February 25, 2016 18:48
-
-
Save anonymous/ea15982d040265dfc71b to your computer and use it in GitHub Desktop.
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 | |
PEOPLE=[] | |
attr_reader :name, :hobbies, :friends | |
def initialize(name) | |
@name=name | |
@hobbies= [] | |
@friends= [] | |
PEOPLE<<self | |
end | |
def has_hobby(hobby) | |
@hobbies << hobby | |
end | |
def has_friend(friend) | |
@friends << friend | |
end | |
def self.method_missing(m,*args) | |
method=m.to_s | |
if method.start_with?("all_with_") | |
attr=method[9..-1] | |
if self.public_method_defined(attr) | |
PEOPLE.find_all do |person| | |
person.send(attr).include?(args[0]) | |
end | |
else | |
raise ArgumentError, "Cant find #{attr}" | |
end | |
else | |
super | |
end | |
end | |
end | |
mor=Person.new("mor") | |
mor.has_friend("yossi") | |
p Person.all_with_yossi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment