RubyMine 6.3.0 Web Help

When the Extract Method refactoring is invoked , RubyMine analyses the selected block of code and detects variables that are the input for the selected code fragment and the variables that are output for it.

Example

Before After
                  name = "RubyMine"
puts "Hello world from #{name}"
						         
                  name = "RubyMine"
                  def greet name
    puts "Hello world from #{name}"
                  end

greet(name)
    
                  def 
                  foo
                  a = 0
                  b = 1
                  c = 2
                  return 
                  a * b  + c * 123
                  end
						         
                  def 
                  bar(a, b, c)
                  return 
                  a * b  + c * 123
                  end
                  def 
                  foo
                  a = 0
                  b = 1
                  c = 2
                  return  bar(a, b, c)
                  end
						         
                  class 
                  Bar
                  def 
                  foo
                            a = 11 + 2
                            b = 11 + 2
                  end
                  end
                        
                  class Bar
    def 
                  foo
                  a = addition()
     b = addition()
    end
                  def addition
   11+2
                  end
                  end
                       
To extract a method
  1. In the editor, select a block of code to be transformed into a method or a function.

    Tip

    The code fragment to form the method does not necessarily have to be a set of statements. It may also be an expression used somewhere in the code.

  2. On the main menu or on the context menu of the selection, choose Refactor | Extract | Method or press Ctrl+Alt+MCtrl+Alt+MCtrl+Alt+MCtrl+Alt+MCtrl+Alt+MCtrl+Alt+MShift+Alt+MShift+Alt+MAlt+Meta+MAlt Command MAlt Command MAlt Command MAlt Command M.
  3. In the Extract Method dialog box that opens, specify the name of the new method.
  4. In the Parameters area, do the following:
    • Specify the variables to be passed as method parameters by selecting/clearing the corresponding check boxes.
    • Rename the desired parameters, by double-clicking the corresponding parameter lines and entering new names.
  5. Check the result in the Signature Preview pane and click OK to create the method.

    Tip

    6.0+
    If duplicate code fragments are encountered, RubyMine suggests to replace them with the calls to the extracted method:

    rm_extract_method_duplicates

    The selected code fragment will be replaced with a method call.

See Also

Reference:

Web Resources: