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?
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
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).
You need to understand that the code block (wrapped by either { … } or do … end,) is itself an object, that is an argument to the add_item() method, so must be part of the same statement.