Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `+=` with `send` method

Tags:

ruby

send

How can one use send with +=?

a = 20; a.send "+=", 10
undefined method `+=' for 20:Fixnum

a = 20; a += 10
=> 30
like image 533
JZ. Avatar asked Dec 11 '12 22:12

JZ.


1 Answers

I'm afraid you cannot. += is not a method, but rather syntactic sugar.

See http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html

It says

In common with many other languages, Ruby has a syntactic shortcut: a=a+2 may be written as a+=2.

Best you can do is:

>> a = 20
=> 20
>> a = a.send "+", 10
=> 30
>> a
=> 30
like image 156
Ray Toal Avatar answered Oct 05 '22 23:10

Ray Toal