question about defined? and y

R

Ruby Freak

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
 
A

alandacosta

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

Igal Koshevoy

Aditya said:
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.
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
 
B

Ben Bleything

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
 
R

Robert Klemme

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
 
B

Ben Bleything

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
 
R

Ruby Freak

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

J. Cooper

Ruby said:
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 :)
 
B

Ben Bleything

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
 
R

Ruby Freak

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

Ben Bleything

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)

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
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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".
 
S

Sean O'Halpin

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

$ ruby -e "puts defined?(y); require 'yaml'; puts defined?(y)"
nil
method

Regards,
Sean
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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 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. <G>
 
R

Robert Klemme

[Note: parts of this message were removed to make it a legal post.]

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

"Falsy" isn't there...

http://www.websters-online-dictionary.org/definition/falsy

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

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top