[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

self.module_eval?

Douglas Livingstone

4/3/2005 9:33:00 PM

Why does this work:

class ComplexObject
attr_accessor :colour
def initialize # :yield: c
ComplexObject.module_eval "undef_method :colour="
end
end

But this doesn't?

class ComplexObject
attr_accessor :colour
def initialize # :yield: c
self.module_eval "undef_method :colour="
end
end

(btw, I know about attr_reader, I'm just looking for ways around the
"disadvantages" for "Initializing complex objects" at
http://www.rubygarden.org/ruby?... )

I assumed that self == ComplexObject, but obviously not... why is that?

Cheers,
Douglas


1 Answer

James Gray

4/3/2005 9:42:00 PM

0


On Apr 3, 2005, at 4:33 PM, Douglas Livingstone wrote:

> Why does this work:
>
> class ComplexObject
> attr_accessor :colour
> def initialize # :yield: c
> ComplexObject.module_eval "undef_method :colour="
> end
> end
>
> But this doesn't?
>
> class ComplexObject
> attr_accessor :colour
> def initialize # :yield: c
> self.module_eval "undef_method :colour="
> end
> end
>
> (btw, I know about attr_reader, I'm just looking for ways around the
> "disadvantages" for "Initializing complex objects" at
> http://www.rubygarden.org/ruby?... )
>
> I assumed that self == ComplexObject, but obviously not... why is that?

When initialize() gets called, "self" will be setup to point to the
current object, as is always true with Ruby instance methods. If you
want to fetch the object's class, without hardcoding it, use
"self.class..."

Hope that helps.

James Edward Gray II