From: "aDAVISk (Akito D. Kawamura)" Date: 2022-04-15T11:09:56+00:00 Subject: [ruby-core:108262] [Ruby master Feature#18736] self-p for method chain Issue #18736 has been updated by aDAVISk (Akito D. Kawamura). That's exactly I meant. Thanks a lot, I set this topic closed. ---------------------------------------- Feature #18736: self-p for method chain https://bugs.ruby-lang.org/issues/18736#change-97283 * Author: aDAVISk (Akito D. Kawamura) * Status: Open * Priority: Normal ---------------------------------------- You may want to check object with `p` method at the middle of a method chain. Here is my recomendation. ```ruby class Object def sp(method=nil, *args, &block) if method Kernel.p self.public_send(method, *args, &block) elsif block_given? Kernel.p block.call(self) else Kernel.p self end return self end end ``` Example of usage: 1) check itself in the middle of a method-call ```ruby p [1,2,3].map{|x| x**2}.sp.map{|x| x**2} ``` - output ``` [1,4,9] [1,16,81] ``` 2) check its value in some string format ```ruby [1,2,3].sp{|x| "my List = #{x}"} ``` - output ``` "my List = [1,2,3]" ``` 3) check its sum with an initial value ```ruby [1,2,3].sp(:sum,-10) ``` - output ``` -4 ``` 4) check its value with a `map` operation ```ruby [1,2,3].sp(:map){|x| x**2} ``` - output ``` [1,4,9] ``` 5) check its value connected in string ```ruby [1,2,3].sp(:sum,""){|x| x.to_s} ``` - output ``` "123" ``` --- Your brush-up comments are welcomed. thx. -- https://bugs.ruby-lang.org/ Unsubscribe: