[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

question about defined? and y

Ruby Freak

6/26/2008 5:47:00 PM

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

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

Thanks

18 Answers

Aditya Mahajan

6/26/2008 5:59:00 PM

0

alandacosta@gmail.com

6/26/2008 6:01:00 PM

0

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

Igal Koshevoy

6/26/2008 6:42:00 PM

0

Aditya Mahajan wrote:
> On Thu, 26 Jun 2008, Ruby Freak wrote:
>> The uninitialized variable y returns from defined? as "method" rather
>> than "nil" ????
>>
>> puts defined?(x) # => nil
>>
>> puts defined?(y) # => method
>
> 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

Ben Bleything

6/26/2008 8:32:00 PM

0

On Fri, Jun 27, 2008, alandacosta@gmail.com 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

Robert Klemme

6/26/2008 9:15:00 PM

0

On 26.06.2008 22:32, Ben Bleything wrote:
> On Fri, Jun 27, 2008, alandacosta@gmail.com 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

Ben Bleything

6/26/2008 9:22:00 PM

0

On Fri, Jun 27, 2008, Robert Klemme 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

Ruby Freak

6/27/2008 12:17:00 AM

0


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

6/27/2008 3:32:00 AM

0

Ruby Freak 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 :)
--
Posted via http://www.ruby-....

Ben Bleything

6/27/2008 4:02:00 PM

0

On Fri, Jun 27, 2008, Ruby Freak 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

Ruby Freak

6/27/2008 5:56:00 PM

0

On Jun 27, 7:01 am, Ben Bleything <b...@bleything.net> 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/methodCheckDe...

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.