On Thu, May 1, 2008 at 11:54 PM, James B.
[email protected] wrote:
Can you explain to me the different behaviours between defined? and
respond_to? in this instance. I am not clear why defined? would work
with symbolized path names but not work with the same parameterized
respond_to? asks whether the object responds to some method. Since you
want to know whether self responds to the path generator that’s the
defined? is more generic. That’s a special operator in the sense that
Ruby does not evaluate its argument. I don’t know the whole story
(anyone?) but a posteriori defined? works at parse time or something
like that to interpret the argument expression in a special way.
For example, if SOME_CONSTANT is unknown
does not raise NameError, it does not evaluate the argument. It
silently returns nil. If the constant was known it would return
Check this other one for example:
irb(main):004:0> a = “\n”
irb(main):005:0> defined? a.chop!
You see? chop! is not being called, we are just told that a responds to
I’d like to provide a better explanation of defined? but don’t have
Flanagan & Matz at hand.