super
with no arguments, you'll be passed the same arguments as the current method? "[^ assign]: If you just act a destructive method instead of an assignment, the original and new objects are the same, so this question is out of the question.
I didn't know, so I experimented.
class Foo
def mtd(str)
puts "[Foo:1] str => %p (%#08x)" % [str, str.__id__]
end
end
class Bar < Foo
def mtd(str)
puts "[Bar:1] str => %p (%#08x)" % [str, str.__id__]
str *= 2
puts "[Bar:2] str => %p (%#08x)" % [str, str.__id__]
super
end
end
Bar.new.mtd("abc")
result
[Bar:1] str => "abc" (0x00003c) <--The original object passed as a formal argument
[Bar:2] str => "abcabc" (0x000050) <--Object reassigned and overwritten (different ID)
[Foo:1] str => "abcabc" (0x000050) <--Result: Overwritten is used
If you write only super
, it complements the feeling ofsuper (str)
, and as a result, it is better to reassign.
Just in case, when I looked at the reference manual, it was written firmly in ** Sample Code ** .. (It is difficult because it can be interpreted in either way if it is a textual explanation)
Recommended Posts