Creating and Customizing Ruby Code Templates

See Also

You can customize the code templates used in the Ruby code editor. Code templates are abbreviations that expand into snippets of code when you press Tab after typing the snippet name in the editor.

To customize the list of code templates:

  1. Choose Tools > Options.
  2. Click Editor and select the Code Templates tab.
  3. Select a one of the Ruby options from the Language drop-down list to display its the code template list.
  4. Edit, create, and remove templates as needed.

Special Code Template Syntax

When you use the Options window to create Ruby code templates, there are several constructs that you can use to customize the way the code template behaves. The table below lists some of these constructs.

Code Template Syntax Construct Explanation
${cursor} Indicates where the insertion point should go after the code snippet has been added to your code.
${Identifier} Indicates an identifer that needs to be filled in when you use the code template. When you use this construct in a template definition, replace Identifier with the identifer name that you want to appear in the template.
index An attribute that you can use within a ${Identifier} segment to designate that an unused variable name should be generated in the code snippet. For example, you could use ${ind index}.
${selection} Defines a position for pasting a content of the editor selection. Used by selection templates, which appear as hints whenever you select some text in the editor (see next item).
${selection line allowSurround} Defines a selection template by using the allowSurround parameter. When there is an editor selection, a lightbulb is presented to the user, and all the surround-with code templates are listed as possible fixes.
${param_name default="value"} Defines the parameter's default value. Set to empty to create a tab stop.
${param_name editable=false} Can be used to disable user's editing of the parameter. Useful for creating caret locations (tab stops).
${class} Expands to the name of the class surrounding the template expansion location, such as Foo
${classfqn} Same as ${class} , but instead of just the class name expands to the fully qualified class name, such as Bar::Baz::Foo
${superclass} Expands to the name of the superclass of the class surrounding the template expansion location, such as SuperFoo
${method} Expands to the name of the method surrounding the template expansion location, such as foo
${methodfqn} Same as ${method} , but instead of just the class method expands to the fully qualified name, such as Bar::Baz::Foo#foo
${file} Expands to the name of the current file, such as foo_bar.rb
${path} Expands to the name of the current full file path, such as /foo/bar/baz.rb
${unusedlocal defaults="i,j,k"} Picks the name of an unused local variable in the current scope. It will first try each of the candidates listed in the defaults attribute, and if none of those succeed, create another unique name.
See Also
Working With NetBeans Ruby
About Ruby Development in NetBeans

Legal Notices