-
-
Save arslanfarooq/54ac5aa7796385e4a063 to your computer and use it in GitHub Desktop.
From "Well Grounded Rubyist" by David A. Black,
Listing 4.11 Simple usage of the Person Class (page 107),
Listing 4.12 Implementation of the main logic of the Person class (page 108),
Listing 4.13 Full implementation of Person.method_missing (page 109),
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
j = Person.new("Julia") | |
p = Person.new("Peter") | |
g = Person.new("Gabriel") | |
r = Person.new("Rebecca") | |
j.has_friends(p) | |
j.has_friends(r) | |
g.has_friends(p) | |
r.has_hobbies("Stargazing") | |
Person.all_with_friends(p).each do |person| | |
puts "#{person.name} is friends with #{p.name}" | |
end | |
Person.all_with_hobbies("Stargazing").each do |person| | |
puts "#{person.name} is into Stargazing" | |
end |
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_hobbies(hobby) | |
@hobbies << hobby | |
end | |
def has_friends(friend) | |
@friends << friend | |
end |
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
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, "Can't find #{attr}" | |
end | |
else | |
super | |
end | |
end | |
end |
You can modify the gist to reflect the change.
Btw. I would change the has_XYZ methods to return self. The current implementation leaks the Array member.
rklemme thank you! I edited the gist.
About the has_XYZ methods... and how I should add return self line, could you please explain the Array leaking part? It would help me understand things.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
4.12 line 7 and 8, doing this fixes it:
@Hobbies = []
@friends = []
and this is how it was in the book :-|, I made a mistake while typing...