Skip to content

Instantly share code, notes, and snippets.

@arslanfarooq
Last active December 19, 2015 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arslanfarooq/54ac5aa7796385e4a063 to your computer and use it in GitHub Desktop.
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),
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
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
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
@arslanfarooq
Copy link
Author

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...

@rklemme
Copy link

rklemme commented Jul 9, 2013

You can modify the gist to reflect the change.

@rklemme
Copy link

rklemme commented Jul 9, 2013

Btw. I would change the has_XYZ methods to return self. The current implementation leaks the Array member.

@arslanfarooq
Copy link
Author

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