Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby, if we define "c=(foo)" and it returns foo + 1, why is this not assigned to d = (self.c = 3)?

Tags:

ruby

the code is

def c=(foo)
  p "hello"
  return foo + 1
end

p self.c = 3
d = (self.c = 3)
p d

and it will print out 3 only... in other words, the returned value 4 is not assigned to d, why?

like image 235
nonopolarity Avatar asked Dec 28 '22 01:12

nonopolarity


1 Answers

Setters return their argument (or right operand, depending on how you look at it) — always.

like image 58
Chuck Avatar answered May 14 '23 06:05

Chuck