# If assignment is a method call

**URL:** <https://www.ruby-forum.com/t/if-assignment-is-a-method-call/113056>\
**Category:** Ruby\
**Created:** [August 27, 2007, 3:00am UTC](https://www.ruby-forum.com/t/if-assignment-is-a-method-call/113056 "2007-08-27T03:00:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [August 27, 2007, 3:00am UTC](https://www.ruby-forum.com/t/if-assignment-is-a-method-call/113056/1 "2007-08-27T03:00:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [August 27, 2007, 8:40am UTC](https://www.ruby-forum.com/t/if-assignment-is-a-method-call/113056/2 "2007-08-27T08:40:36Z")

</div>

On 8/27/07, Nasir K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
