# Simplifying the defined api

**URL:** <https://www.ruby-forum.com/t/simplifying-the-defined-api/117158>\
**Category:** Ruby\
**Created:** [October 14, 2007, 1:14am UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158 "2007-10-14T01:14:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 14, 2007, 1:14am UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158/1 "2007-10-14T01:14:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 14, 2007, 6:47am UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158/2 "2007-10-14T06:47:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 14, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158/3 "2007-10-14T14:51:21Z")

</div>

On 10/13/07, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 15, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158/4 "2007-10-15T18:44:37Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 15, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158/5 "2007-10-15T23:03:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 15, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158/6 "2007-10-15T23:34:19Z")

</div>

On 10/13/07, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 15, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158/7 "2007-10-15T23:37:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 17, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158/8 "2007-10-17T22:25:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [October 15, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/simplifying-the-defined-api/117158/9 "2007-10-15T23:05:43Z")

</div>

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
