[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: if assignment is a method call

Robert Dober

8/27/2007 6:39:00 AM

On 8/27/07, Nasir Khan <rubylearner@gmail.com> wrote:
> Given an object x, if you invoke x.y = 1 then this will work if the object
> has an attribute y and is writable (or you have defined a method y= )
> If that is not the case then you will get a NoMethodError. You can override
> the method_missing() method and capture this.
> When you invoke a method on an object you can also attach a block to the
> invocation. e.g object.method(arg1, arg2) { puts "in the block" }
> Furthermore when a method_missing is invoked for missing method the block is
> available to it - method_missing(m,*a, &block).
>
> Now the question is that though y= behaves like a method and is captured by
> the method_missing as well, there is no way to provide a block to the method
> call.
>
> x.y=(1) { puts "in the block"} is actually a compile error.
>
> irb(main):005:0* x.y=(1) { puts "in the block"}
> SyntaxError: compile error
> (irb):5: syntax error, unexpected '{', expecting $end
> x.y=(1) { puts "in the block"}
>
> Is there a way to pass a block to the assignment?
x.send( :y=, 1) do puts "in the block" end
HTH
Robert

>
> Thanks
> Nasir
>


--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn