Naming of "defined?" 
Author Message
 Naming of "defined?"

Didn't i read that '?' at the end of a method name indicated a boolean
return value?

defined? returns the symbol type, not a boolean.

Therfore, this sort of confusion:


#!/usr/bin/ruby

def i
  p "defining i as method"
end
p defined? i

i = 0 if not defined? i
p defined? i


"method"
"local-variable"



Wed, 11 Jun 2003 00:38:53 GMT  
 Naming of "defined?"
See below...



Quote:
> Didn't i read that '?' at the end of a method name indicated a boolean
> return value?

> defined? returns the symbol type, not a boolean.

> Therfore, this sort of confusion:


> #!/usr/bin/ruby

> def i
>   p "defining i as method"
> end
> p defined? i

> i = 0 if not defined? i
> p defined? i


> "method"
> "local-variable"

Disclaimer: I don't have a Ruby interpreter handy at the moment.

What if i really is undefined? Doesn't it return false or nil?

And really, that is adequate for a Boolean, there being no Boolean
type as such in Ruby. The fact that it returns different meaningful
values for truth could be viewed as just a bonus.

Still, it's nice to know that you can distinguish in this way between
a method name and a local variable... I've just never seen anyone do it
as far as I can recall.

Hal

--
Hal Fulton

Sent via Deja.com
http://www.deja.com/



Wed, 11 Jun 2003 02:04:32 GMT  
 Naming of "defined?"

Quote:
> I had a problem on page 221 of Programming Ruby,
> where it indicates that "defined?" is a method... Dave,
> could you explain? (Maybe I should have said something sooner)

You think _you_ had a problem on page 221 ;-)

Actually, that's a bug. Maybe. We _call_ it an operator consistently,
and the tick in the method column says that it can be overridden.
That's only half true. You _can_ override it, but you can't easily
_use_ the new method:

  def defined?(a)
    puts "new"
  end
  puts defined?(1)       #=> "expression"
  public :defined?
  puts self.defined?(1)  #=> new

So I'm not sure what to do: shall we remove the tick or not?

Confused of Dallas



Wed, 11 Jun 2003 03:14:35 GMT  
 Naming of "defined?"

Quote:

> Didn't i read that '?' at the end of a method name indicated a boolean
> return value?

> defined? returns the symbol type, not a boolean.

Well, first of all, "defined?" is not a method.

cf. ruby-talk: 01637, where matz answers Quinn Dunkan:
|Ah, this makes more sense, defined? isn't a method at all.  I thought
|everything was a message send in ruby? :)  It *could* be "just another message
|send" if you spelled it 'defined? :foo' or 'defined? "foo"'.  So why not?

No it isn't.  It's a control structure.  No everything is a message
send in Ruby, e.g. control structures, variables, blocks are not
objects.  `defined? ' is among these things.

`defined?' shows

  * if a global variable is defined
  * if a local variable is defined
  * if a instance variable is defined
  * if a constant is defined
  * if a method is defined
  * if a block is supplyed
  * if a method of same name in superclass is defined

all of which is not accomplished by a method.
<<

Quote:
> Therfore, this sort of confusion:


> #!/usr/bin/ruby

> def i
>   p "defining i as method"
> end
> p defined? i

> i = 0 if not defined? i
> p defined? i


> "method"
> "local-variable"

I think your confusion is in
1) thinking defined? is a method
2) thinking methods ending in '?' have to be boolean

Both of which are false.

I had a problem on page 221 of Programming Ruby,
where it indicates that "defined?" is a method... Dave,
could you explain? (Maybe I should have said something sooner)

Guy N. Hurst

--
HurstLinks Web Development    http://www.hurstlinks.com/
Norfolk, VA - (757)623-9688
PHP/MySQL - Ruby/Perl - HTML/Javascript



Wed, 11 Jun 2003 03:03:13 GMT  
 Naming of "defined?"

Quote:


> > I had a problem on page 221 of Programming Ruby,
> > where it indicates that "defined?" is a method... Dave,
> > could you explain? (Maybe I should have said something sooner)

> You think _you_ had a problem on page 221 ;-)

> Actually, that's a bug. Maybe. We _call_ it an operator consistently,
> and the tick in the method column says that it can be overridden.
> That's only half true. You _can_ override it, but you can't easily
> _use_ the new method:

>   def defined?(a)
>     puts "new"
>   end
>   puts defined?(1)       #=> "expression"
>   public :defined?
>   puts self.defined?(1)  #=> new

> So I'm not sure what to do: shall we remove the tick or not?

> Confused of Dallas

It seems more like an co-ride than an override, at least taking the
latter term in a possibly sub-rigorous, semi-intuitive sense.  You can
certainly do similar things with other things that are not "ticked
off" on p. 221:

   irb(main):001:0> def not
   irb(main):002:1> puts "No way"
   irb(main):003:1> end
   nil
   irb(main):004:0> public :not
   Object
   irb(main):005:0> self.not
   No way
   nil
   irb(main):006:0> not 3
   false
   irb(main):007:0>

... so I'm not sure what defined?'s tick has going for it.

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Wed, 11 Jun 2003 07:37:40 GMT  
 Naming of "defined?"

Quote:
> .. so I'm not sure what defined?'s tick has going for it.

Well, I hear it plays a mean game of pool, and the other ticks quite
like it, but...

It's outa there.

Dave



Wed, 11 Jun 2003 09:17:08 GMT  
 Naming of "defined?"
Hi,

In message "[ruby-talk:7961] Naming of "defined?""

|Didn't i read that '?' at the end of a method name indicated a boolean
|return value?

The '?' methods are methods designed to be used as predicates, not
methods which CAN be used as predicate.  They return either

  (a) true or false
  (b) non-false informative value or nil

`defined?' falls into (b).

In addition, as a rare exception, Thread#alive? returns true for
unterminated threads, false for normally terminated threads, and nil
for threads terminated by exceptions.

                                                        matz.



Wed, 11 Jun 2003 11:30:54 GMT  
 Naming of "defined?"

Quote:

> Hi,

> In message "[ruby-talk:7961] Naming of "defined?""

> |Didn't i read that '?' at the end of a method name indicated a boolean
> |return value?

> The '?' methods are methods designed to be used as predicates, not
> methods which CAN be used as predicate.  They return either

>   (a) true or false
>   (b) non-false informative value or nil

> `defined?' falls into (b).

> In addition, as a rare exception, Thread#alive? returns true for
> unterminated threads, false for normally terminated threads, and nil
> for threads terminated by exceptions.

>                                                         matz.

I made a mistake in thinking there was something wrong with the `not
defined?' predicate in:


#!/usr/bin/ruby

def i
  p "defining i as method"
end
p defined? i

i = 0 if not defined? i
p defined? i


"method"
"local-variable"

I assumed that "i = 0" must have been executed because something that
_had_ been defined as a method was now seen as a local-variable.  But
that's not what happened.  The predicate _did_ fail but just the
appearance of "i = 0" (not executed) made the method "disappear".  So
i'm just trying to understand some of the quirky aspects of this.  I
thought that if a later _assignment_ of a variable could override a
method definition, then a later method definition could override a
variable "declaration".  But (i see) that's not exactly what happened
and . . . it doesn't work that way.  It's one (and only one) or the
other, but the interpreter can change it's mind about which one it is --
variables seeming to have preference over methods this way.

There are instances when a symbol is assumed to be a method and then
found to actually be a variable reference.  Is there any point, though,
in allowing methods to be redefined as variables?  I would think that
(as suggested) warnings in the former case could be issued and, in the
latter, it would be an error.  But then, i've only been playing with
Ruby for a couple of days and maybe this behavior is actually useful.

craig



Wed, 11 Jun 2003 11:57:09 GMT  
 Naming of "defined?"

Quote:

> I made a mistake in thinking there was something wrong with the `not
> defined?' predicate in:


> #!/usr/bin/ruby

Use the -w flag -- that way if matz introduces new warnings,
you'll be able to see them :-)

Quote:
> def i
>   p "defining i as method"
> end
> p defined? i

> i = 0 if not defined? i
> p defined? i


> "method"
> "local-variable"

But note what happens if you put parens after that last i:

   ....
   p defined? i()

output:

   "method"
   "method"

Here's a few more little test lines, in irb:

   irb 1> def i
   irb 2> "method i"
   irb 3> end
      ==>nil
   irb 4> public :i
      ==>Object
   irb 5> i = 123
      ==>123
   irb 6> self.i
      ==>"method i"
   irb 7> i
      ==>123
   irb 8> defined? self.i
      ==>"method"
   irb 9> defined? i
      ==>"local-variable"

So the local variable i masks, but does not actually replace, the
method i (as I understand it).

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Wed, 11 Jun 2003 13:03:02 GMT  
 Naming of "defined?"
.
.
.

Quote:

> But note what happens if you put parens after that last i:

>    ....
>    p defined? i()

> output:

>    "method"
>    "method"

Oh . . . _now_ i get it!  They're both there.  You just can't refer to a
method __without paren's__ if you've referenced a variable by the same
name.  That makes me feel so much better!

craig



Wed, 11 Jun 2003 13:16:19 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

2. define "variable" word

3. Redesigning "define interface"

4. defining "|" as or in Poplog Prolog

5. Lets define "pointer"

6. Lets define "pointer"

7. defining "preferences" in cosmo with code

8. use defined PROTO in "Browser.createVrmlFromString"?

9. +define+unknown="'bx" chokes verilog

10. Defining "public" functions

11. "#define" and C2Ada

12. best way of defining a "timer"

 

 
Powered by phpBB® Forum Software