When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used

[^ 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

When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used
If it is Ruby, it is efficient to make it a method and stock the processing.
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
How to output the value when there is an array in the array
A memorial service for the library used when competing in Ruby
Throw an exception and catch when there is no handler corresponding to the path in spring
I want to pass the argument of Annotation and the argument of the calling method to aspect
[Ruby] How to prevent errors when nil is included in the operation
How to reference a column when overriding the column name method in ActiveRecord
[Ruby] I want to make an array from a character string with the split method. And vice versa.
How to check if an instance variable is defined in a Ruby class
Cause of is not visible when calling a method of another class in java
How to mock a super method call in PowerMock
How to perform a specific process when the back button is pressed in Android Fragment
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
How to specify an array in the return value / argument of the method in the CORBA IDL file
How to solve the problem when the value is not sent when the form is disabled in rails and sent
[Rails] What to do when the view collapses when a message is displayed with the errors method
Java learning_Behavior when there is a method with the same name as a field with the same name in two classes that have an inheritance relationship
[Ruby] Count an even number in an array using the even? Method
Determine that the value is a multiple of 〇 in Ruby
How to change a string in an array to a number in Ruby
I want to call a method and count the number
How to retrieve the hash value in an array in Ruby
Create a method to return the tax rate in Java
What is the difference between an action and an instance method?
Monkey patch to return the current time when an empty string is specified in Ruby's Time.parse
About the method to convert a character string to an integer / decimal number (cast data) in Java
Settings to be made when HTML or JSP is formatted in Eclipse and the layout is disappointing
[Java] How to search for a value in an array (or list) with the contains method
If you want to mock a method in RSpec, you should use the allow method for mock and the singleton method.
Method definition location Summary of how to check When defined in the project and Rails / Gem
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
[Ruby] I want to put an array in a variable. I want to convert to an array
[Ruby] Hash # has_key? Is one step closer to the official deprecated?
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to install the language used in Ubuntu and how to build the environment
How to store the information entered in textarea in a variable in the method
Things to remember and concepts in the Ruby on Rails tutorial
Easy to understand the difference between Ruby instance method and class method.
How to write the view when Vue is introduced in Rails?
Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
[Ruby On Rails] In the create action and destroy action, emergency measures when redirect_to action:: show cannot be (cannot be used)
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
What to do if Operation not permitted is displayed when you execute a command in the terminal
[Swift] Rather, it is meaningful to avoid var and declare it with let only when there is a branch in the assignment of the initial value.