Simplifying the defined api

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

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.

On 10/13/07, Roger P. [email protected] 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

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

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

On 10/13/07, Roger P. [email protected] 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.denhaven2.com/

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?

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

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