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 a caret at a string containing the required value.
From the main menu, select
Ctrl+Alt+P.(Optional) If you place a 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: 23 April 2021