Extract parameter
The Extract Parameter refactoring is used to add a new parameter to a method declaration and to update the method calls accordingly.
The animation below demonstrates how to add a method parameter by extracting a value from a string:
To extract a parameter:
Select a value to refactor or place the caret at a string containing the required value.
In the main menu, go to
Ctrl+Alt+P.(Optional) If you place the caret at a place containing several expressions available for extracting, select the required expression and press Enter:
In the invoked dialog, specify the parameter name and type:
You can use various parameter types such as positional, array, keyword and so on.
Click Refactor.
Example
Before | After |
---|---|
def greet
puts "Hello from JetBrains"
end
greet
|
def greet(name)
puts "Hello from #{name}"
end
greet("JetBrains")
|
Last modified: 04 April 2024