From: Trans Date: 2006-12-21T20:36:57+09:00 Subject: Re: Assignment method strangeness Ryan Williams wrote: > > > > As you can see in the first one.. even a single value passed to an > > assignment is parsed as an array. If you need multiple arguments to an > > assignment, you have to handle them inside the method body itself. > > > > > You are correct. That's pretty useful, though you still can't pass in > blocks. > > The reason I was interested in this was that I wanted to create some > attributes that could be assigned either a literal value, or a block that > returned the literal value when called. Then you could do some really lazy > evaluation, as in: > > test.a = { long_computation } > .... > ...in another scope... > .... > test.a # returns result of computation > > Or you could use it to keep some objects in sync: > > test.a = { some_other_object.b } > some_other_object.b = 42 > test.a = 42 > > I guess the best route to this is either a new assignment-like method ( > test.set_a) or test.a = Proc.new. class Test def a=(x) case x when Proc puts "proc!" else puts "value" end end end test = Test.new test.a = lamba { some_other_object.b } also if you do need more than one arg: def a(x=Exception, y=nil) return @a if x == Exception @a = x + y # or whatever end t.