# Ruby19 and defined?

**URL:** <https://www.ruby-forum.com/t/ruby19-and-defined/132588>\
**Category:** Ruby\
**Created:** [March 21, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/ruby19-and-defined/132588 "2008-03-21T18:12:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 21, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/ruby19-and-defined/132588/1 "2008-03-21T18:12:49Z")

</div>

This seems to be a change in 1.9 that’s breaking some code (it was  
reported by a user of the bit-struct library). Here’s the test case:

class C; def self.foo; end; end  
class D\<C  
p superclass  
p defined?(superclass.foo)  
p defined?(superclass.fooz)  
end

$ ruby -v bug.rb  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-linux]  
C  
“method”  
nil  
$ ruby19 -v bug.rb  
ruby 1.9.0 (2007-12-25 revision 14709) [i686-linux]  
C  
true  
true  
$ ruby19svn -v bug.rb  
ruby 1.9.0 (2008-03-21 revision 15824) [i686-linux]  
C  
true  
true

I don’t see anything quite relevant in  
[eigenclass.org](http://eigenclass.org/hiki/Changes+in+Ruby+1.9).

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 21, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/ruby19-and-defined/132588/2 "2008-03-21T18:53:50Z")

</div>

I know there was an RCR related to this once. Maybe it was accepted.  
GL.  
-R

> $ ruby -v bug.rb  
> ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-linux]  
> C  
> “method”  
> nil  
> $ ruby19 -v bug.rb  
> ruby 1.9.0 (2007-12-25 revision 14709) [i686-linux]  
> C  
> true  
> true

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 21, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/ruby19-and-defined/132588/3 "2008-03-21T18:57:53Z")

</div>

> > > > > “J” == Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

J\> This seems to be a change in 1.9 that’s breaking some code

a bug always break some code 🙂

There are many chance that it’s a bug.

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 22, 2008, 10:53am UTC](https://www.ruby-forum.com/t/ruby19-and-defined/132588/4 "2008-03-22T10:53:35Z")

</div>

Roger P. wrote:

> I know there was an RCR related to this once. Â Maybe it was accepted.

You mean an RCR proposing that defined? return true, right?  
The problem here is not that defined? returns true instead of “method”  
though.  
The problem is that it returns true instead of nil for the method that  
doesn’t  
exist. I doubt that that behaviour was proposed in an RCR.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 22, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/ruby19-and-defined/132588/5 "2008-03-22T14:13:31Z")

</div>

On 3/22/08, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Roger P. wrote:
> 
> > I know there was an RCR related to this once. Maybe it was accepted.
> 
> You mean an RCR proposing that defined? return true, right?  
> The problem here is not that defined? returns true instead of “method” though.  
> The problem is that it returns true instead of nil for the method that doesn’t  
> exist. I doubt that that behaviour was proposed in an RCR.

Hmmmm, not so sure that that’s actually a bug.

I’ve not found a good definition for what define? is supposed to do  
when given an expression instead of a simple term. It certainly never  
evaluated the expression. I don’t know that I’ve ever used it with an  
expression, but my impression was that it works syntactically more  
than semantically, so whether or not a given expression would raise an  
exception when evaluated wouldn’t affect whether the expression was  
defined. For example, I’d expect that

```
 defined?(1/0)

```

would not indicate that the expression was undefined just because it  
represents a divide by zero.

Having just looked at the latest beta of Pickaxe 3, I notice that Dave  
still has define? returning strings instead of true/nil, and the page  
has the red header indicating that it’s been updated for 1.9.

Personally, I’m unhappy if 1.9 is no longer returning a description of  
the syntactical class of the argument to defined? seems like a loss of  
valuable information, and I’d hope that it was the returning of nil  
rather than ‘method’ which is a bug.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [March 22, 2008, 2:21pm UTC](https://www.ruby-forum.com/t/ruby19-and-defined/132588/6 "2008-03-22T14:21:48Z")

</div>

Rick DeNatale wrote:

> Hmmmm, not so sure that that’s actually a bug.

It’s a bug

Here the bytecode

vgs% ./ruby -ve ‘puts  
VM::InstructionSequence.compile(“defined?(a.b)”).disasm’  
ruby 1.9.0 (2008-03-21 revision 15825) [i686-linux]  
:1: warning: useless use of defined? in void context  
== disasm: \<ISeq:@\>=================================  
0000 putself (  
1)  
0001 defined 21, :a, false  
0005 leave  
0006 pop  
0007 branchif 12  
0009 putnil  
0010 leave  
0011 pop  
0012 putnil  
0013 send :a, 0, nil, 24,   
0019 defined 13, :b, true  
0023 leave  
vgs%

Now if I add ‘p’

vgs% ./ruby -ve ‘puts VM::InstructionSequence.compile(“p  
defined?(a.b)”).disasm’  
ruby 1.9.0 (2008-03-21 revision 15825) [i686-linux]  
== disasm: \<ISeq:@\>=================================  
0000 putnil (  
1)  
0001 putself  
0002 defined 21, :a, false  
0006 jump 24  
0008 branchif 13  
0010 putnil  
0011 jump 24  
0013 putnil  
0014 send :a, 0, nil, 24,   
0020 defined 13, :b, true  
0024 send :p, 1, nil, 8,   
0030 leave  
vgs%

See the bytecode ‘jump 24’ in 0006

Guy Decoux
