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
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
self.module_eval?
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password