From: Rick DeNatale Date: 2007-09-24T20:49:27+09:00 Subject: Re: foo || 0 will cause exception but not for himself On 9/23/07, Phrogz wrote: > On Sep 22, 2:51 pm, 7stud -- wrote: > > Gavin Kistner wrote: > > > On Sep 22, 7:26 am, SpringFlowers AutumnMoon > > > 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 => # 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.denhaven2.com/