what's wrong with this snippet ...

J

Jimmy Kofler

Hi, all!
I've just stumbled over this orphaned snippet on my hard drive that
returns a NoMethodError message instead of "Method meow called"!
Is there a workaround?
Thanks!


AnimalSounds = Module.new

AnimalSounds.class_eval %q!
def createSound(attr_name)
class_eval <<-EOS
def #{attr_name}; puts "Method #{attr_name} called"; end
EOS
end!

Pet = Object.clone.extend(AnimalSounds)
Pet.createSound("meow")

fido = Pet.clone
fido.meow # undefined method `meow' for #<Class:0x53c04>
(NoMethodError)
 
V

Vincent Fourmond

Jimmy said:
Hi, all!
I've just stumbled over this orphaned snippet on my hard drive that
returns a NoMethodError message instead of "Method meow called"!
Is there a workaround?
Thanks!


AnimalSounds = Module.new

AnimalSounds.class_eval %q!
def createSound(attr_name)
class_eval <<-EOS
def #{attr_name}; puts "Method #{attr_name} called"; end
EOS
end!

This creates an instance method and not a class method. Consider using
def self.#{attr_name}.

Cheers,

Vince
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top