Question about defined? and y

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/

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

Thanks

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

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).

On Fri, Jun 27, 2008, [email protected] 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 :slight_smile:

Ben

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

On 26.06.2008 22:32, Ben B. wrote:

On Fri, Jun 27, 2008, [email protected] 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

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

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.

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 :slight_smile:

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 :slight_smile:

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

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

On Jun 27, 7:01 am, Ben B. [email protected] 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

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.

On Fri, Jun 27, 2008 at 1:57 PM, Ruby F. [email protected]
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/

On 29.06.2008 11:18, Marc H. wrote:

truthy and falsy?

Is this baby-talk? :slight_smile:

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

robert

truthy and falsy?

Is this baby-talk? :slight_smile:

On Thu, Jun 26, 2008 at 6:48 PM, Ruby F. [email protected]
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

On Sun, Jun 29, 2008 at 6:27 AM, Robert K.
[email protected]
wrote:

On 29.06.2008 11:18, Marc H. wrote:

truthy and falsy?

Is this baby-talk? :slight_smile:

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 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/

On Sun, Jun 29, 2008 at 5:18 AM, Marc H. [email protected]
wrote:

truthy and falsy?

Is this baby-talk? :slight_smile:

It’s duck-talk :slight_smile:


Avdi

Home: http://avdi.org
Developer Blog: Avdi Grimm, Code Cleric
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com

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. :slight_smile:

It turns out that the word “truthy” has some history. According to this
Wikipedia article Truthiness - Wikipedia 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

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

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

Cheers

robert