Skip to content

Instantly share code, notes, and snippets.

@carlwoodward
Created November 23, 2008 06:50
Show Gist options
  • Save carlwoodward/28045 to your computer and use it in GitHub Desktop.
Save carlwoodward/28045 to your computer and use it in GitHub Desktop.
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
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