Actions
Feature #18736
openself-p for method chain
Status:
Open
Assignee:
-
Target version:
-
Description
You may want to check object with p
method at the middle of a method chain.
Here is my recomendation.
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:
- check itself in the middle of a method-call
p [1,2,3].map{|x| x**2}.sp.map{|x| x**2}
- output
[1,4,9]
[1,16,81]
- check its value in some string format
[1,2,3].sp{|x| "my List = #{x}"}
- output
"my List = [1,2,3]"
- check its sum with an initial value
[1,2,3].sp(:sum,-10)
- output
-4
- check its value with a
map
operation
[1,2,3].sp(:map){|x| x**2}
- output
[1,4,9]
- check its value connected in string
[1,2,3].sp(:sum,""){|x| x.to_s}
- output
"123"
Your brush-up comments are welcomed. thx.
Actions
Like0
Like0Like0Like0