RubyMine 2022.2 Help

Create files and directories

RubyMine enables you to create arbitrary files or language-specific files. You can create Ruby files, classes or modules, tests, HTML files, stylesheets, JavaScript/TypeScript files, and so on. Such language-specific files can be created with initial content using templates. For example, the RSpec test created from a template contains a skeleton for an example group. If necessary, you can customize these templates or add new ones.

In this topic, we'll show you how to create files and directories, and how to use templates.

Create a Ruby file, class, or module

RubyMine enables you to create Ruby files or generate classes and modules. To create a new Ruby file, class, or module, do the following:

  1. In the Project view Alt+1, select the directory where you want a class to be created, and press Alt+Insert.

  2. From the popup menu, select Ruby File/Class.

  3. In the New Ruby File/Class popup, do one of the following:

    • To create a new Ruby file, enter its name and make sure that File is selected.

      New Ruby File/Class dialog
    • To create a Ruby class, switch to Class and specify the class name.

      New Ruby File/Class dialog

      RubyMine allows you to create classes prepended by modules. If required, prepend the class name with the module name:

      New Ruby File/Class dialog

      In this case, the created file will look as follows:

      New class
    • To create a Ruby module, switch to Module and specify the module name.

    Press Enter.

Create a directory

To create a directory:

  1. In the Project view Alt+1, right-click the directory where you want to create a new one and select New | Directory from the context menu. Alternatively, click the parent directory, press Alt+Insert, and then select Directory from the list.

  2. In the popup that opens, specify the directory name. To create several nested directories, use slashes as separators, for example, views/shared.

Create an empty file

To create an empty file with an arbitrary extension:

  1. In the Project view Alt+1, select the directory where you want to create a file, press Alt+Insert, and then select File from the list.

  2. In the New File dialog that opens, type the filename and extension. You can type the whole directory structure before the new filename. If the nested directories do not yet exist, they will be created.

  3. If the specified extension is not associated with any file type recognized in RubyMine, the Register New File Type Association dialog opens where you can associate the extension with one of the recognized file types. See File type associations for details.

File templates

RubyMine allows you to create new files with initial content using templates. You can customize these templates or add new ones.

Customize file templates

  1. Press Ctrl+Alt+S to open the IDE settings and select Editor | File and Code Templates.

  2. On the Files tab, select the desired template and modify it:

    • Edit the template contents. Since file templates use the Velocity Template Language, you can use a fixed text, variables, or various directives, including #parse, #set, #if, and others.

    • Reformat according to style: Enable this option to reformat the generated code according to the style defined on the Code Style page.

    • Enable Live Templates: Enable this option if you want to use a live template inside a file template. For example, to specify the caret position in the created HTML file inside body, put the $END$ live template variable into Velocity escape syntax:

      <!DOCTYPE html> <html lang="en"> <head></head> <body> #[[$END$]]# </body> </html>
  3. Click Apply to save the customized template. If necessary, click Reset to Default to revert the selected template to its original state.

  4. To copy the existing template or create a new one, use the Copy Template or Create Template buttons, respectively.

Share code between templates

Include templates allow you to create reusable pieces of code, for example, require statements. Let's see on how to create the include template shared between the Minitest templates:

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Editor | File and Code Templates.

  2. Open the Includes tab.

  3. Click the Create Template button on the toolbar and specify the template settings in the following way:

    • Name: Specify the template name as Minitest File Header.

    • Extension: Leave the default rb extension.

    • Add the following template body:

      require 'minitest/autorun'

    Click Apply.

  4. Go to the Files tab.

  5. For the MiniTest and MiniTest Spec templates, replace the require 'minitest/autorun' line with the following code:

    #parse("Minitest File Header.rb")

    Click Apply.

Save the opened file as a template

  1. Choose File | Save File as Template from the menu.

  2. In the Save File as Template dialog that opens, specify the new template name and edit the body, if necessary.

  3. Apply the changes and close the dialog.

File template variables

A file template can contain variables, which are replaced by their values when the template is applied. A variable is a string that starts with a dollar sign $ followed by the variable name. The variable name may optionally be enclosed in curly braces. For example, $MyVariable and ${MyVariable} are different notations of the same variable.

Predefined template variables

The following predefined variables can be used in file templates:

Variable

Description

${DATE}

Current system date

${DAY}

Current day of the month

${DIR_PATH}

Path to the directory of the new file (relative to the project root)

${DS}

Dollar sign $. This variable is used to escape the dollar character, so that it is not treated as a prefix of a template variable.

${FILE_NAME}

Name of the new file

${HOUR}

Current hour

${MINUTE}

Current minute

${SECOND}

Current second

${MONTH}

Current month

${MONTH_NAME_FULL}

Full name of the current month (January, February, and so on)

${MONTH_NAME_SHORT}

First three letters of the current month name (Jan, Feb, and so on)

${NAME}

Name of the new entity (file, %class%, interface, and so on)

${PRODUCT_NAME}

Name of the IDE (for example, RubyMine)

${PROJECT_NAME}

Name of the current project

${TIME}

Current system time

${USER}

Login name of the current user

${YEAR}

Current year

Custom template variables

Besides predefined template variables, it is possible to specify custom variables. If necessary, you can define the values of custom variables right in the template using the #set directive. Write the directive before the corresponding variable is used.

For example, if you want to use your full name instead of your login name defined through the predefined variable ${USER}, add the following construct before your custom variable:

#set( $MyName = "John Smith" )

If the value of a variable is not defined in the template, RubyMine will ask you to specify it when the template is applied.

Last modified: 09 August 2022