If assignment is a method call

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?

Thanks
Nasir

On 8/27/07, Nasir K. [email protected] wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs