Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the method symbol for += in ruby?

Tags:

ruby

x + y 

is syntactic sugar for

x.send(:+, y)

What is this a syntactic sugar for?

x += y

I've tried

x.send(:+=, y)

but it doesn't work

like image 855
syclee Avatar asked Apr 20 '15 11:04

syclee


People also ask

What is a Ruby method?

A method in Ruby is a set of expressions that returns a value. Within a method, you can organize your code into subroutines which can be easily invoked from other areas of their program. A method name must start a letter or a character with the eight-bit set.

Is a method an object in Ruby?

From ruby's perspective Methods are Objects. The implementation details are not important. A method can be addresses and passed just like any other object.

What is the at symbol in Ruby?

In Ruby, the at-sign ( @ ) before a variable name (e.g. @variable_name ) is used to create a class instance variable.


1 Answers

It is not a method. It is a short way (syntactic sugar) for writing following:

x = 1
#=> 1
x += 1 # same as x = x + 1
#=> 2
like image 86
Andrey Deineko Avatar answered Sep 28 '22 05:09

Andrey Deineko