• Kushal Mistry

Rightward Assignment in Ruby

let's take a quick tour of the new experimental feature in ruby called Rightward Assignments.


EARLIER

We all know that to assign a value to a variable it is mentioned as,

a = 3x + y

this pattern is followed from Mathematics to almost all programming languages. to assign a value to variable we assign Rvalue to Lvalue as indicated above.


while the above pattern has become standardized, it feels somewhat unnatural as we read most of the spoken languages from left to right.

we were not able to do

33 => students

NOW

with recent changes, Ruby supports Right ward assignments. let's take a look at some examples below,

5 => x # this is equivalent to x = 5

let's take a look at few more examples,


Example 1

[155, 44] => height, weight

which is equivalent to

height, weight = [155, 44]

Example 2

def traits
  [170, 65]
end

traits => height, weight

Note : This feature is added in Ruby as an experimental feature. it can be removed if developers are not satisfied with it.

 

©2020 by Kushal Mistry