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

code

def c=(foo) p "hello" return foo + 1 end p self.c = 3 d = (self.c = 3) pd 

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

+4
source share
2 answers

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

+7
source

Methods like def foo=(bar) cannot return a value. Probably to enable multitasking:

 a = self.foo = 3 # a will be 3, independent of what foo returns 
+3
source

Source: https://habr.com/ru/post/1341228/


All Articles