What's wrong with this snippet

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)

Jimmy K. wrote:

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

Vincent F. wrote:

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

Thanks, Vince!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs