Ruby Code Editor Manual

The following is a manual for the latest version of the Ruby Code Editor (RCE) for SketchUp. It is assumed that you have successfully installed this extension (instructions are on the previous page).

On This Page

    Main Screen Elements

    1. You can access most functions of this editor from its menu. For example, go to File > Open… to load a file into the editor.
    2. The current filename is shown on this tab (file path appears when you hover over it). At present, only one file can be loaded at a time, but future versions will allow you to edit multiple files at the same time.
    3. This is the editor window. By default, a set of default lines are loaded that you can use as starting point for your code. You can adjust the appearance of this editor in the RCE preferences.
    4. The Run button executes the code in the editor window in your current SketchUp session.
    5. The Undo button undoes the last code executions (or other edits), just as SketchUp’s regular undo would. By default, the entire code in the editor window is executed in one undo step (so that it can be reversed easily). You can adjust this behavior in the RCE preferences.
    6. This dropdown contains several snippets (general Ruby and SketchUp API specific). Place the cursor in the editor where you would like one of these inserted and then select the code to be inserted.
    7. You can adjust the syntax highlighting of the editor window using this dropdown. Included highlighters are: None, (SketchUp) Ruby, JavaScript, HTML, CSS, XML.
    8. This section narrates the editor’s behavior and shows the Ruby output (similar to what would be shown in the Ruby Console). Not all of the Ruby error messages are repeated here, however (only the most crucial ones). If you want to get all feedback, also open the Ruby Console and leave it open (at the side) when you execute code.

    Some Notes:

    • All code is executed at the toplevel binding, i.e. under main.
    • Because the code is executed from the editor window (even if it was originally loaded from a file), __FILE__ does not return the current filename. This is the same behavior as the Ruby Console itself. If you need a filename or directory in your code, then you should supply that otherwise.
    • If needed, you can add up to two directory paths to the $LOAD_PATH variable in Ruby by entering those into the Add Ruby Search Paths section of the RCE preferences. This is necessary if your code has require statements. See the Preferences section for more details.
    • You can execute code in a Ruby file (using load) with the Run (Load) File menu item under the Run menu. This allows you to work with code that has require statements. Just edit a file, save it, and then execute it using this command instead of the normal Run Code.
    • When code highlighting fails (e.g. only a section of the code is properly colored), then that is usually due to too many operators being located too close to each other for the highlighter to keep them apart. Add spaces to fix this (e.g. at the arrow location shown below).
    • Pressing Ctrl/Cmd+Shift in the editor brings up a code completion dropdown. This is very “alpha-level” rudimentary functionality at this point. I will improve this in one of the next versions.

    Menu Options

    File Menu:

    • New – Clears the editor and loads the default code snippet.
    • Open… – Loads a file into the editor.
    • Open Recent – Shows the five most recently opened files for quick access.
    • Save – Saves the currently opened file.
    • Save As… – Allows you to specify where to save the editor contents.
    • Print Code – Prints the editor contents with syntax highlighting. Tip: If you want to turn off syntax highlighting, select “None” from the syntax highlighting options before printing.
    • Exit Editor – Closes this RCE window (only affects the current window).

    Edit Menu:

    • Undo – Undoes the last code execution (similar to the Undo button).
    • Redo – Repeats the last code execution (similar to SketchUp’s Edit > Redo menu item).
    • Find – Opens the search dialog (at the top of the RCE editor window).
    • Find Next – Jumps to the next found occurrence.
    • Find Previous – Jumps to the previous found occurrence.
    • Replace – Opens the search and replace dialog (at the top of the editor window).
    • Replace All – Replaces every occurrence of a search term.
    • Clear Search – Clears the current Find or Replace operation.
    • Edit Default Code – Loads the default code into the editor and lets you edit it. This is the code that appears when you select File > New.

    Run Menu:

    • Run Code – Executes the code in the editor (similar to the Run button).
    • Undo – Undoes the last code execution (similar to the Undo button).
    • Run (Load) Code – Executes code in an external file using Ruby’s load method. You need to pick the external file each time you use this menu item. It does not load anything into the RCE.

    Selection Menu:

    • List Properties – Lists all properties for the currently selected model objects. Select one or more items first (step 1), then select this menu item (step 2), then view all properties (step 3). If needed you can even copy those from the dialog.
    • List Attributes – Similar to List Properties, but displays all attributes in all attribute directories for the selected object(s).
    • Insert ID Reference(s) – Inserts SketchUp’s numeric ID references for the selected objects at the current cursor position into the editor.

    Tools Menu:

    • Show Ruby Console – Opens SketchUp’s Ruby Console.
    • Show Reference Browser – Opens RCE’s reference browser window, which can be used to look up items in the SketchUp API or in other general Ruby sources. Tip: On Windows this can be docked side-by-side with the editor window:
    • Open Plugin/Extension Folder – Opens your local “Plugins” folder with your OS file tool (File Explorer or Finder). This is just a convenient way to open the location that contains all your installed extension files. It does not load anything into the RCE.
    • Preferences – Those are covered in the subsequent section…

    Help Menu

    • Help – Opens a browser window to this site where you can look up this manual, leave comments and ask questions.
    • Keyboard Shortcuts – Displays a list of keyboard shortcuts in RCE.
    • About – Shows the current version and some legal stuff.

    Preferences

    You can adjust the RCE’s preferences here:

    • User interface style – Select between a dark and a light version. I may add more styles to this later.
    • Font size – Allows you to change the font size in the editor.
    • Indent with tabs – By default the editor indents with spaces; you can switch that to tabs here. Tip: You always indent with the Tab key. If you would like to indent multiple lines of code, highlight all and then hit the Tab key.
    • Indent size – If indenting with spaces, how many should be used.
    • Smart indenting (language dependent) – Indents based on the selected language settings.
    • Show line numbers – Shows or hides line numbers in the editor.
    • Always save backup file (with BAK ending) – Adds a backup file whenever Save is selected.
    • Wrap entire code in single undo – You can change this behavior here to a line-by-line execution record.
    • Add Ruby Search Paths – If needed, you can add up to two directory paths to the $LOAD_PATH variable in Ruby by entering those here. This is necessary if your code has require statements. When code is executed, files in these paths can then be found by Ruby.
      The entered paths will be saved with the RCE and will appear here until they are deleted from these options. Paths are dynamically added to the $LOAD_PATH variable when Run is selected. Also, entered paths are processed through Ruby’s File.expand_path method. Therefore, you can insert a tilde ~ to represent the user’s home directory, if desired.

    Editor Keyboard Shortcuts

    • Ctrl/Cmd + N : New
    • Ctrl/Cmd + O : Open
    • Ctrl/Cmd + S : Save
    • Ctrl/Cmd + P : Print Code
    • Ctrl/Cmd + F4 : Exit Editor
    • Ctrl/Cmd + R : Run Code
    • Ctrl/Cmd + U : Undo Run
    • Ctrl/Cmd + L : Run (Load) File
    • Ctrl/Cmd + Space : Autocomplete
    • Ctrl/Cmd + Z : Undo Edit
    • Tab : Indent More (selection)
    • Shift + Tab : Indent Less (selection)
    • Ctrl/Cmd + Q : Fold Code
    • Ctrl/Cmd + F : Search
    • Alt + F : Search (persistent)
    • Ctrl/Cmd + G : Find next
    • Shift + Ctrl + G : Find previous
    • Shift + Ctrl + F : Replace
    • Shift + Ctrl + R : Replace all
    • Alt + G : Jump to line

    Comments and Reactions