[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

simplifying the defined api

Roger Pack

10/13/2007 11:15:00 PM

Question: A recent suggestion to ruby was to have all
defined?
method's return true or nil.
So my question is...does anybody ever use "defined?" for anything
besides true/nil?

I also wonder if the complex defined? api explains why this code:
assert(defined?(a) and defined?(b))
throws a syntax error for some reason.
Any thoughts welcome.
Take care.
-Roger
--
Posted via http://www.ruby-....

8 Answers

Marc Heiler

10/14/2007 4:47:00 AM

0

> I also wonder if the complex defined? api explains why this code:
> assert(defined?(a) and defined?(b))


I use defined? but I never had the need to use assert.
--
Posted via http://www.ruby-....

Austin Ziegler

10/14/2007 12:51:00 PM

0

On 10/13/07, Roger Pack <rogerpack2005@gmail.com> wrote:
> I also wonder if the complex defined? api explains why this code:
> assert(defined?(a) and defined?(b))
> throws a syntax error for some reason.

No. That's an unfortunate situation with the "and" and "or" in Ruby:

>> def foo?(ob)
>> ob.to_s == "foo"
>> end
=> nil
>> foo?("foo") and foo?("bar")
=> false
>> def baz(truth)
>> puts "You can't handle the truth."
>> end
=> nil
>> baz(foo?("foo") and foo?("bar"))
SyntaxError: compile error
(irb):8: syntax error, unexpected kAND, expecting ')'
baz(foo?("foo") and foo?("bar"))
^
(irb):8: syntax error, unexpected ')', expecting $end
from (irb):8
>> baz(foo?("foo") && foo?("bar"))
You can't handle the truth.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Roger Pack

10/15/2007 4:45:00 PM

0



> I use defined? but I never had the need to use assert.
Did anyone ever use defined? for anything other than 'true/nil'
(true/false)?
That's what I wonder...:)
-Roger
--
Posted via http://www.ruby-....

mortee

10/15/2007 9:03:00 PM

0

Roger Pack

10/15/2007 9:06:00 PM

0

in ruby you can do
defined? local_variable_name
or defined? ClassName
or what not and it tells you if those things exist.

mortee wrote:
> Roger Pack wrote:
>> Question: A recent suggestion to ruby was to have all
>> defined?
>> method's return true or nil.
>> So my question is...does anybody ever use "defined?" for anything
>> besides true/nil?
>
> It looks like I'm missing something... What's the "defined? api"?
>
> mortee

--
Posted via http://www.ruby-....

Rick DeNatale

10/15/2007 9:31:00 PM

0

On 10/13/07, Roger Pack <rogerpack2005@gmail.com> wrote:
> Question: A recent suggestion to ruby was to have all
> defined?
> method's return true or nil.

Let me turn this around.

Why care that it returns truth values other than literal true?

The interpretation of objects as truth values is consistent within
ruby, defined? is one of cases in which that is used to advantage to
convey additional information.


> So my question is...does anybody ever use "defined?" for anything
> besides true/nil?

Sure, it's useful to know that the syntactic properties of an
expression are dynamically.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Roger Pack

10/15/2007 9:37:00 PM

0


> Let me turn this around.
>
> Why care that it returns truth values other than literal true?

I think the rationale was it would make Ruby simpler and therefore one
less thorn in the side for interpreters to implement. Good point,
though.


> Sure, it's useful to know that the syntactic properties of an
> expression are dynamically.

question: allocated dynamically?
--
Posted via http://www.ruby-....

Roger Pack

10/17/2007 8:25:00 PM

0

So is this a parser bug or a feature?

> baz(foo?("foo") and foo?("bar"))
> ^
> (irb):8: syntax error, unexpected ')', expecting $end
> from (irb):8
>>> baz(foo?("foo") && foo?("bar"))
> You can't handle the truth.
>
> -austin
--
Posted via http://www.ruby-....