# Question about defined? and y

**URL:** <https://www.ruby-forum.com/t/question-about-defined-and-y/141529>\
**Category:** Ruby\
**Created:** [June 26, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529 "2008-06-26T19:50:51Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 26, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/1 "2008-06-26T19:50:51Z")

</div>

The defined? keyword seems to have some funky behaviors.  
I have decided that the best way to use it is to compare the output to  
nil as it returns a myriad of results that all result in truth.

puts (defined?(x) == nil ? “Not defined” : “Is defined”)

but…  
What’s up with y?

The uninitialized variable y returns from defined? as “method” rather  
than “nil” ???

puts defined?(x) # =\> nil

puts defined?(y) # =\> method

At least this happens on my buddy’s win XP box that I am using, to  
play with Ruby. Maybe the box is spanked as this doesn’t seem to  
happen in the online ruby demo at [http://tryruby.hobix.com/](http://tryruby.hobix.com/)

I am sure there is a stupid simple answer, but it eludes me.

Thanks

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 26, 2008, 8:01pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/2 "2008-06-26T20:01:33Z")

</div>

On Thu, 26 Jun 2008, Ruby F. wrote:

> than “nil” ???
> 
> puts defined?(x) # =\> nil
> 
> puts defined?(y) # =\> method
> 
> At least this happens on my buddy’s win XP box that I am using, to  
> play with Ruby.

Are you testing this on irb or by writing on a file and running through  
ruby? When I test this on irb, both x and y are defined as methods.  
While  
running through ruby, both are nil.

Aditya

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 26, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/3 "2008-06-26T20:06:14Z")

</div>

Try:

!!defined? x

The “!!” should take care of your nil situation when you want a true/  
false. If “defined?(y)” is returning “method”, that means you  
previously def’ed y … maybe even in your .irbrc file (if called from  
irb).

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 26, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/4 "2008-06-26T22:35:20Z")

</div>

On Fri, Jun 27, 2008, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The “!!” should take care of your nil situation when you want a true/  
> false. If “defined?(y)” is returning “method”, that means you  
> previously def’ed y … maybe even in your .irbrc file (if called from  
> irb).

But I’m not sure when you ever actually _care_ that it’s either true  
or false. nil is false, false is false, everything else is true. The  
result is that you generally needn’t care what defined? returns.

I’ve never seen a case where this isn’t true, but of course that does  
not mean they don’t exist 🙂

Ben

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 26, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/5 "2008-06-26T20:45:00Z")

</div>

Aditya M. wrote:

> methods. While running through ruby, both are nil.  
> If you’re using an IRB with certain settings, such as the Rails console,  
> it includes the “y” method as a wrapper providing functionality similar  
> to:

```
def y(*args)
    require 'yaml'
    print YAML::dump(args)
end

```

This command is useful for inspecting data structures.

-igal

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 26, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/6 "2008-06-26T23:15:48Z")

</div>

On 26.06.2008 22:32, Ben B. wrote:

> On Fri, Jun 27, 2008, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > The “!!” should take care of your nil situation when you want a true/  
> > false. If “defined?(y)” is returning “method”, that means you  
> > previously def’ed y … maybe even in your .irbrc file (if called from  
> > irb).
> 
> But I’m not sure when you ever actually _care_ that it’s either true  
> or false. nil is false, false is false, everything else is true. The  
> result is that you generally needn’t care what defined? returns.

I would go even as far as to not care what defined? actually does. I  
did not have use for it yet. I have observed that when I started Ruby  
with a bit of Perl background I felt that I needed it but quickly  
discovered that not. So, OP what do you need defined? for?

Cheers

robert

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 26, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/7 "2008-06-26T23:24:35Z")

</div>

On Fri, Jun 27, 2008, Robert K. wrote:

> I would go even as far as to not care what defined? actually does. I did  
> not have use for it yet. I have observed that when I started Ruby with a  
> bit of Perl background I felt that I needed it but quickly discovered that  
> not. So, OP what do you need defined? for?

That’s true. I generally assume if people are asking about a thing they  
have a legitimate need, but you’re right that defined? is one of those  
things that isn’t needed that often.

Ben

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 27, 2008, 2:20am UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/8 "2008-06-27T02:20:44Z")

</div>

> But I’m not sure when you ever actually _care_ that it’s either true  
> or false. nil is false, false is false, everything else is true. The  
> result is that you generally needn’t care what defined? returns.

Problem is “method” isn’t false, so it comes back as non-nil, or true.

I am running this in Scite with nothing defined for y that I know of.  
The whole file is:

puts defined?(x) # =\> nil

puts defined?(y) # =\> method

Soo.. I guess y is defined somewhere  
and Defined? is pretty much non-dependable.  
I was just following a pedantic exercise so I don’t really need it,  
but the failure made the exercise confusing.

Thanks.

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 27, 2008, 5:34am UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/9 "2008-06-27T05:34:26Z")

</div>

Ruby F. wrote:

> Problem is “method” isn’t false, so it comes back as non-nil, or true.
> 
> I am running this in Scite with nothing defined for y that I know of.  
> The whole file is:
> 
> puts defined?(x) # =\> nil
> 
> puts defined?(y) # =\> method
> 
> Soo.. I guess y is defined somewhere  
> and Defined? is pretty much non-dependable.  
> I was just following a pedantic exercise so I don’t really need it,  
> but the failure made the exercise confusing.
> 
> Thanks.

Try calling y() in that file 🙂

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 27, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/10 "2008-06-27T18:04:23Z")

</div>

On Fri, Jun 27, 2008, Ruby F. wrote:

> Problem is “method” isn’t false, so it comes back as non-nil, or true.

Right, which means that y is defined… and it is. I don’t see the  
problem 🙂

> Soo.. I guess y is defined somewhere  
> and Defined? is pretty much non-dependable.  
> I was just following a pedantic exercise so I don’t really need it,  
> but the failure made the exercise confusing.

How is it undependble, and what is the failure?

Ben

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 27, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/11 "2008-06-27T20:27:36Z")

</div>

On Sat, Jun 28, 2008, Ruby F. wrote:

> is probably just my imagination)  
> I think this is the case. For one thing, variables are effectively just  
> pointers. y is defined, and it points at a method. The predicate is  
> returning something that can be treated as a boolean which is sort of  
> the duck-typing way.

I kind of like the way that defined? behaves, because it gives you more  
information than a simple boolean would. That said, I understand your  
frustration as well, because it is surprising that it would tell you  
about methods.

Ben

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 27, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/12 "2008-06-27T20:27:36Z")

</div>

On Jun 27, 7:01 am, Ben B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How is it undependble, and what is the failure?
> 
> Ben  
> well, I guess it works, but  
> I find it undependable in the fact that it is not actually doing what  
> I expect. I expect that it will tell me if a variable has been, well,  
> defined as a variable. In the case of the unused variable “y”, I  
> expect that it would tell me that the variable “y” has not been used.  
> What I get back is the fact that “y” is a method. That has some useful  
> aspects, but it is not what I am after or what I expected.

Possibly I simply have inaccurate expectations. For whatever reason, I  
expect a method/keyword that ends in “?” to return a boolean. (which  
is probably just my imagination)

In reality, I don’t really need to use defined?, I just wanted to  
understand why “defined?(y)” was not returning nil, and that has been  
answer.

I am somewhat forming my opinion on the discussion here:  
[http://redhanded.hobix.com/inspect/methodCheckDefined.html](http://redhanded.hobix.com/inspect/methodCheckDefined.html)

Were Why the Lucky Stiff says:

“At heart, the defined? keyword is something of a hack, a rather rare  
anomaly in Ruby’s syntax. Since an undefined variable is a perfectly  
legal argument, the defined? keyword couldn’t be implemented as a  
method. Which means that this keyword is left out of Ri documentation.  
The Pickaxe groups it with operators. So, it often gets overlooked.”

Thanks all.

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 29, 2008, 12:41am UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/13 "2008-06-29T00:41:23Z")

</div>

On Fri, Jun 27, 2008 at 1:57 PM, Ruby F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Possibly I simply have inaccurate expectations. For whatever reason, I  
> expect a method/keyword that ends in “?” to return a boolean. (which  
> is probably just my imagination)

In general, predicates in Ruby return an object which can be regarded as  
“truthful” or not.

Lately, after reading Douglas Crockford’s “JavaScript: The Good Parts,”  
I’ve  
taken to adapting his explanation of “truthy” and “falsy” values to  
Ruby. I  
like this because it put’s Stephen Colbert’s notion of “truthiness” into  
practice.

## In Ruby nil and false are the only “falsy” values, any other object is “truthy”.

Rick DeNatale

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

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 29, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/14 "2008-06-29T12:30:47Z")

</div>

On 29.06.2008 11:18, Marc H. wrote:

> truthy and falsy?
> 
> Is this baby-talk? 🙂

I think it’s just the attempt to find words that cannot be confused with  
“true” and “false” (the objects).

robert

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 29, 2008, 11:21am UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/15 "2008-06-29T11:21:32Z")

</div>

truthy and falsy?

Is this baby-talk? 🙂

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 29, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/16 "2008-06-29T13:44:00Z")

</div>

On Thu, Jun 26, 2008 at 6:48 PM, Ruby F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> than “nil” ???
> 
> puts defined?(x) # =\> nil
> 
> puts defined?(y) # =\> method

$ ruby -e “puts defined?(y); require ‘yaml’; puts defined?(y)”  
nil  
method

Regards,  
Sean

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 29, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/17 "2008-06-29T14:47:11Z")

</div>

On Sun, Jun 29, 2008 at 6:27 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> On 29.06.2008 11:18, Marc H. wrote:
> 
> > truthy and falsy?
> > 
> > Is this baby-talk? 🙂
> 
> I think it’s just the attempt to find words that cannot be confused with  
> “true” and “false” (the objects).

Well it is that, but…

It turns out that the word “truthy” has some history. According to this  
Wikipedia article [Truthiness - Wikipedia](http://en.wikipedia.org/wiki/Truthiness) it appears in  
the  
O.E.D. along with “Truthiness” which was popularized recently in the US  
by  
political satirist Stephen Colbert as a term for the property of  
something  
being known instinctively from the (political) “gut” without regard to  
evidence, or facts.

I don’t have access to the O.E.D. but my 1978 edition of Webster’s New  
20th  
Century Dictionary of the English Languate (unabridged) defines truthy  
as a  
dialect variation of truthful.

Falsy is just a similar construction, chosen no doubt so as to avoid  
confusion with falsie a word usually used in the plural which has an  
entirely different meaning.

–  
Rick DeNatale

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

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 29, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/18 "2008-06-29T18:01:11Z")

</div>

On Sun, Jun 29, 2008 at 5:18 AM, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> truthy and falsy?
> 
> Is this baby-talk? 🙂

It’s duck-talk 🙂

–  
Avdi

Home: [http://avdi.org](http://avdi.org)  
Developer Blog: [avdi.codes – Avdi Grimm, Code Cleric](http://avdi.org/devblog/)  
Twitter: [http://twitter.com/avdi](http://twitter.com/avdi)  
Journal: [http://avdi.livejournal.com](http://avdi.livejournal.com)

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [June 30, 2008, 8:00pm UTC](https://www.ruby-forum.com/t/question-about-defined-and-y/141529/19 "2008-06-30T20:00:59Z")

</div>

On 29.06.2008 14:43, Rick DeNatale wrote:

> > > 
> > 
> > I think it’s just the attempt to find words that cannot be confused with  
> > “true” and “false” (the objects).
> 
> Well it is that, but…

Interesting stuff that you dug up there. 🙂

> It turns out that the word “truthy” has some history. According to this  
> Wikipedia article [Truthiness - Wikipedia](http://en.wikipedia.org/wiki/Truthiness) it appears in the  
> O.E.D. along with “Truthiness” which was popularized recently in the US by  
> political satirist Stephen Colbert as a term for the property of something  
> being known instinctively from the (political) “gut” without regard to  
> evidence, or facts.
> 
> I don’t have access to the O.E.D. but my 1978 edition of Webster’s New 20th  
> Century Dictionary of the English Languate (unabridged) defines truthy as a  
> dialect variation of truthful.

Webster’s online version does not give much insight:  
[http://www.websters-online-dictionary.org/definition/truthy](http://www.websters-online-dictionary.org/definition/truthy)

> Falsy is just a similar construction, chosen no doubt so as to avoid  
> confusion with falsie a word usually used in the plural which has an  
> entirely different meaning.

“Falsy” isn’t there…

[http://www.websters-online-dictionary.org/definition/falsy](http://www.websters-online-dictionary.org/definition/falsy)

… but the link to the different meaning. :-)))

Cheers

robert
