[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

what's wrong with this snippet ...

Jimmy Kofler

1/19/2007 6:37:00 PM


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)

--
Posted via http://www.ruby-....

2 Answers

Vincent Fourmond

1/19/2007 7:03:00 PM

0

Jimmy Kofler wrote:
> 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
--
Vincent Fourmond, PhD student
http://vincent.fourmon...

Jimmy Kofler

1/19/2007 7:53:00 PM

0

> Vincent Fourmond wrote:
>
> This creates an instance method and not a class method. Consider using
> def self.#{attr_name}.
>


Thanks, Vince!

--
Posted via http://www.ruby-....