Setter Method Dates

The following code:

class A
  def foo= foo; puts "b" end
end
A.new.foo=("foo").tap{puts "a"}

prints "a"to "b", which means it tapis called before the setter method foo=. This runs counter to intuition, as I thought the method chain works from left to right. In this example, it appears that the setter method is skipped and runs later. However, it is clear that setter methods are not always executed last in the code. Then, at what point is this foo=, or are setter methods generally executed?

+4
source share
1 answer

The problem is that you are not actually calling the method foo=, but doing the assignment. Ruby interprets it as:

A.new.foo = ("foo").tap{puts "a"}

, 'foo', .

, :

A.new.send(:foo=, 'foo').tap {puts 'a'}
+4

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


All Articles