[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

foo || 0 will cause exception but not for himself

SpringFlowers AutumnMoon

9/22/2007 2:38:00 AM

if foo is undefined, it will throw exception when referred to... as in
try 1, 2, and 3 below. However, it won't throw exception when assigned
to himself... how is that interpreted?


irb(main):001:0> foo
NameError: undefined local variable or method `foo' for main:Object
from (irb):1:in `Kernel#binding'

irb(main):002:0> foo || 0
NameError: undefined local variable or method `foo' for main:Object
from (irb):2:in `Kernel#binding'

irb(main):003:0> bar = foo || 0
NameError: undefined local variable or method `foo' for main:Object
from (irb):3:in `Kernel#binding'

irb(main):004:0> foo = foo || 0
=> 0

irb(main):005:0> foo
=> 0
--
Posted via http://www.ruby-....

7 Answers

Phrogz

9/22/2007 3:02:00 AM

0

On Sep 21, 8:37 pm, SpringFlowers AutumnMoon
<summercooln...@gmail.com> wrote:
> if foo is undefined, it will throw exception when referred to... as in
> try 1, 2, and 3 below. However, it won't throw exception when assigned
> to himself... how is that interpreted?
>
> irb(main):001:0> foo
> NameError: undefined local variable or method `foo' for main:Object
> from (irb):1:in `Kernel#binding'
>
> irb(main):002:0> foo || 0
> NameError: undefined local variable or method `foo' for main:Object
> from (irb):2:in `Kernel#binding'
>
> irb(main):003:0> bar = foo || 0
> NameError: undefined local variable or method `foo' for main:Object
> from (irb):3:in `Kernel#binding'
>
> irb(main):004:0> foo = foo || 0
> => 0
>
> irb(main):005:0> foo
> => 0

When Ruby says "undefined local variable or method", what it means is
"Hey, I don't know what this is, and I don't know if it's supposed to
be a variable or a method."

When you write "foo = ..." Ruby says, "Ah ha! Foo is supposed to be a
variable. I'll treat it as one."

That's my (somewhat informed) guess anyhow. The determination of the
method/variableness of 'foo' is determined before runtime, when the
lexer/parser is analyzing the program. This is similar to why this
code...
if foo=42
p foo
end
works just fine, but this code:
p foo if foo=42
throws an error. When it first see's foo in the first code sample, it
knows that it is a variable. When it first sees foo in the second code
sample, it doesn't know what it is yet, and throws an error to
indicate its confusion.

7stud 7stud

9/22/2007 12:10:00 PM

0

SpringFlowers AutumnMoon wrote:
> if foo is undefined, it will throw exception when referred to... as in
> try 1, 2, and 3 below. However, it won't throw exception when assigned
> to himself... how is that interpreted?
>
> irb(main):004:0> foo = foo || 0
> => 0
>
> irb(main):005:0> foo
> => 0

Maybe your Variable/Method Ambiguity thread applies here?


if false
foo = 10
end

puts foo #nil




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

SpringFlowers AutumnMoon

9/22/2007 1:27:00 PM

0

7stud -- wrote:

> if false
> foo = 10
> end
>
> puts foo #nil

i see...

so after

if false
foo = 10
end

now, bar = foo || 0 will run.

Since for instance methods, the setter method can be foo=()

so how about just a method not inside a class, can it be named "foo=" as
well? if so, how does Ruby know foo=1 is really an assignment or a
method call foo=(1)?

Ruby doesn't seem to like "foo=" as a method:

def foo=(i)
p "ha", i+1
end

def bar?(i)
i % 10
end

p foo=(1)

p foo

p bar?(22)


it will print out

1
1
2

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

Phrogz

9/22/2007 1:50:00 PM

0

On Sep 22, 7:26 am, SpringFlowers AutumnMoon
<summercooln...@gmail.com> wrote:
> so how about just a method not inside a class, can it be named "foo=" as
> well? if so, how does Ruby know foo=1 is really an assignment or a
> method call foo=(1)?
>
> Ruby doesn't seem to like "foo=" as a method:

self.foo = 10

7stud 7stud

9/22/2007 8:52:00 PM

0

Gavin Kistner wrote:
> On Sep 22, 7:26 am, SpringFlowers AutumnMoon
> <summercooln...@gmail.com> wrote:
>> so how about just a method not inside a class, can it be named "foo=" as
>> well? if so, how does Ruby know foo=1 is really an assignment or a
>> method call foo=(1)?
>>
>> Ruby doesn't seem to like "foo=" as a method:
>
> self.foo = 10

def foo=(val)
"hello"
end

puts self.foo = 10 #10
--
Posted via http://www.ruby-....

Phrogz

9/24/2007 2:56:00 AM

0

On Sep 22, 2:51 pm, 7stud -- <dol...@excite.com> wrote:
> Gavin Kistner wrote:
> > On Sep 22, 7:26 am, SpringFlowers AutumnMoon
> > <summercooln...@gmail.com> wrote:
> >> so how about just a method not inside a class, can it be named "foo=" as
> >> well? if so, how does Ruby know foo=1 is really an assignment or a
> >> method call foo=(1)?
>
> >> Ruby doesn't seem to like "foo=" as a method:
>
> > self.foo = 10
>
> def foo=(val)
> "hello"
> end
>
> puts self.foo = 10 #10

Er, what's your point? Methods whose names end with a '=' character
always return the value 'assigned' to them, not the last expression in
the method. How is that relevant to this discussion of how to invoke
them, and the scope and determinatino of variables versus methods?

Rick DeNatale

9/24/2007 11:49:00 AM

0

On 9/23/07, Phrogz <phrogz@mac.com> wrote:
> On Sep 22, 2:51 pm, 7stud -- <dol...@excite.com> wrote:
> > Gavin Kistner wrote:
> > > On Sep 22, 7:26 am, SpringFlowers AutumnMoon
> > > <summercooln...@gmail.com> wrote:
> > >> so how about just a method not inside a class, can it be named "foo=" as
> > >> well? if so, how does Ruby know foo=1 is really an assignment or a
> > >> method call foo=(1)?
> >
> > >> Ruby doesn't seem to like "foo=" as a method:
> >
> > > self.foo = 10
> >
> > def foo=(val)
> > "hello"
> > end
> >
> > puts self.foo = 10 #10
>
> Er, what's your point? Methods whose names end with a '=' character
> always return the value 'assigned' to them, not the last expression in
> the method. How is that relevant to this discussion of how to invoke
> them, and the scope and determinatino of variables versus methods?

Actually, it's not that, it's the way assignments get parsed/compiled:

irb(main):001:0> class Foo
irb(main):002:1> def x
irb(main):003:2> @x
irb(main):004:2> end
irb(main):005:1> def x=(val)
irb(main):006:2> @x = val
irb(main):007:2> "Surprise"
irb(main):008:2> end
irb(main):009:1> end
=> nil
irb(main):010:0> f = Foo.new
=> #<Foo:0x7a1fc>
irb(main):011:0> f.x=2
=> 2
irb(main):012:0> f.__send__(:x=, 3)
=> "Surprise"
irb(main):013:0> f.x
=> 3


--
Rick DeNatale

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