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.
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.
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
···
On Thu, 26 Jun 2008, Ruby Freak wrote:
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.
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 Thu, Jun 26, 2008 at 6:48 PM, Ruby Freak <twscannell@gmail.com> wrote:
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.
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.
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
···
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).
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 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.
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.
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
···
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?
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.
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.
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.
"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 Jun 27, 7:01 am, Ben Bleything <b...@bleything.net> wrote:
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 Sat, Jun 28, 2008, Ruby Freak wrote:
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 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".
···
On Fri, Jun 27, 2008 at 1:57 PM, Ruby Freak <twscannell@gmail.com> 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)
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. <G>
···
On Sun, Jun 29, 2008 at 6:27 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
On 29.06.2008 11:18, Marc Heiler 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).
[Note: parts of this message were removed to make it a legal post.]
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...
Interesting stuff that you dug up there.
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. <G>