You will be able to connect again and use the same method.
class Hoge
def foo
self
end
end
Hoge.new.foo
#<Hoge:0x00007f92612b2e38>
Hoge.new.foo.foo.foo.foo
#<Hoge:0x00007f92618c82a0>
In this way, since the return value is the same type, it is possible to use the same method again.
a.method b
In this situation, the return value will be the receiver a. So
c = a.method b
Then, the same type as a is output to c.
Hold the instance variable and count the number of times the method is called.
class Hoge
def foo
@a.nil? ? @a = 1 : @a = @a + 1
self
end
end
Hoge.new.foo
=> #<Hoge:0x00007f92618c2080 @a=1>
Hoge.new.foo.foo.foo.foo
=> #<Hoge:0x00007f92618a0a98 @a=4>
Then you can see that the value of @a is incremented by the number of times the foo method is called.
Recommended Posts