Sketchup syntax

Hi all,

I have have experience with c++, c#, and unreal blueprints.
I just started learning Ruby, because i want to start to make my one extensions for the company i work for.
But I cant wrap my head around the syntax ruby uses.

for example this works perfectly:

# First we pull in the standard API hooks.
require 'sketchup.rb'

# Show the Ruby Console at startup so we can
# see any programming errors we may make.
SKETCHUP_CONSOLE.show

# Add a menu item to launch our plugin.
UI.menu("Plugins").add_item("Draw stairs") {
  UI.messagebox("I'm about to draw stairs!")
}

But this doesn’t. In my eyes they are the exact same code:

require 'sketchup.rb'
SKETCHUP_CONSOLE.show

UI.menu("Plugins").add_item("Draw stairs") 
{
  UI.messagebox("I'm about to draw stairs!") 
}

can someone explain this to me please?

in short:
c++ read until ;
ruby use the line ending

What you think is the same code isn’t the same because { is on a new line and isn’t part of the command on the previous line.

1 Like

oh waw its just malfunctionning because of the {?
i think this is quite messy but annyway,

thanks for the help Guy!
apprieciate it!

Ruby uses a “do … end” construction that can be replaced with curly braces. However, the first brace must replace where the “do” would appear:

require 'sketchup.rb'
SKETCHUP_CONSOLE.show
UI.menu("Plugins").add_item("Draw stairs") {
  UI.messagebox("I'm about to draw stairs!")
}

is the same as:

require 'sketchup.rb'
SKETCHUP_CONSOLE.show
UI.menu("Plugins").add_item("Draw stairs") do
  UI.messagebox("I'm about to draw stairs!")
end
1 Like

Great,

i think i get ruby’s logic/ syntax now
thanks for the explanation jimhami42

1 Like

You might want to follow @DanRathbun’s suggested framework for an extension (or plugin). He suggests using something like:

#----------------------------------------------------------------------------------------
require 'sketchup.rb'
#----------------------------------------------------------------------------------------
module MilkSnake74
  module TestFile
# MAIN BODY -----------------------------------------------------------------------------
    class  << self
      def test_file()
        UI.messagebox("I'm about to draw stairs!") 
      end
    end
# MAIN BODY -----------------------------------------------------------------------------
    unless file_loaded?("milksnake74_testfile.rb")
      menu = UI.menu("PlugIns").add_item("Test File") { test_file() }
    file_loaded("milksnake74_testfile.rb")
    end
# MAIN BODY -----------------------------------------------------------------------------
  end
end

If you save this as a file named “milksnake74_testfile.rb” in the SketchUp “PlugIns” folder, it should appear on the “Extensions” menu when you next launch SketchUp.

For some examples, feel free to use my plugins (some do not adhere to Dan’s suggested framework and may name-collide with other plugins you have already installed).

Here’s to happy coding :slight_smile:

1 Like

Thats indeed way cleaner!
Thanks for the tip!

You need to understand that the code block (wrapped by either {} or doend,) is itself an object, that is an argument to the add_item() method, so must be part of the same statement.

See this list: Ruby Learning Resources [WikiLists]

You need to learn basic Ruby before learning how the SketchUp API extends Ruby to make plugins.

3 Likes