Created
November 23, 2008 06:50
-
-
Save carlwoodward/28045 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 Dog | |
attr_accessor :animal_type | |
%w(cats dogs rabbits pigs cows).each do |t| | |
define_method "has_#{t}" do |number| | |
@animal_type = t | |
puts number | |
end | |
end | |
end | |
d = Dog.new | |
d.has_dogs 10 | |
puts d.animal_type | |
p d.methods.sort |
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 Dog | |
attr_accessor :number_of_computers | |
def method_missing(name, *args) | |
name = name.to_s | |
name =~ /has_(\d+)_computers/ | |
@number_of_computers = $1.to_i | |
end | |
end | |
d = Dog.new | |
d.has_20_computers | |
puts d.number_of_computers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment