What did I do wrong

I am trying to make the create tubes.rb file that is shown on this page
SketchUp script clips #1: Setup and basic ideas – by [as] (alexschreyer.net)

mod = Sketchup.active_model 
ent = model.active_entities
centerpoint = Geom::Point3d.new 
vector = Geom::Vector3d.new 0,0,1
vector2 = vector.normalize!

(0..20).each do |i|
#create a circle perpendicular to the normal or Z axis
centerpoint = [i*5,sin(i)*10,0]
edges = entities.add_circle centerpoint, vector2, 2
face = entities.add_face(edges)
face.pushpull -i*10
end

When I run the code though I get this message
NameError into String>
C:/Users/boone/AppData/Roaming/SketchUp/SketchUp 2017/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:346:in +' C:/Users/boone/AppData/Roaming/SketchUp/SketchUp 2017/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:346:in rescue in block in initialize’
C:/Users/boone/AppData/Roaming/SketchUp/SketchUp 2017/SketchUp/Plugins/as_rubyeditor/as_rubyeditor.rb:368:in block in initialize' SketchUp:1:in call’

Your snippet has several code errors, mainly involving not matching the name of a variable from one statement to another. Also, sin is not defined at the global level, it is in the Math module and needs a qualifier. As a general comment, no code, even a snippet like this, should define variables at the global level. Always wrap code in a module!

These errors do not explain why you got the specific errors you reported. They ones you saw appear to emerge from the as_rubyeditor, which may be choking while trying to parse your erroneous code.

Try this and see if it does what you intended.

fixed_script.rb (414 Bytes)

Edit: out of curiosity I downloaded as_rubyeditor and took a look at the line called out in the error message you got. I think it is a bug in that extension, ironically similar to the error in your code! While rescuing an Exception, the editor code at line 346 says

r = 'Run aborted. Error: ' + e

but the variable e is not defined at that point in the code, so it can’t be converted to a string to support the + operator. That prevented the editor from reporting the actual cause of the failure of your code.

2 Likes

These errors in these web based snippet editors is the main reason why I suggest not using them.

Again I suggest to use a full-blown code editor and load from a file into SketchUp’s Ruby environment.

2 Likes

1 Like

Thank you. By comparing your script to what I had done, I was able to see what I had done wrong. So, even if I had matched the names it would not have worked.

Is it possible that years ago sin would have worked like he wrote it?
This code was originally posted in 2010. Have rules changed since then?

You have recommended that I use Notepad ++ as a code editor, how do you recommend I test out each line as I am making the file. The only way I have now to test code is copy and paste it into as_rubyeditor.

Download NotePad++, here: Downloads | Notepad++ (notepad-plus-plus.org)

Set the language to Ruby to save the files as .rb for ruby.

A basic set up might look like this (showing two example files, linked below):

You need to put one file, “ex_sphere_tool.rb” into your SketchUp Plugins folder. It should be located like this on your_drive:

C:\Users\your_user_name\AppData\Roaming\SketchUp\SketchUp 2022\SketchUp\Plugins

The other file, “Main.rb”, needs to be in a subfolder of your Sketchup Plugins folder, named, “ex_sphere_tool”.

Here is a link to the SketchUp Ruby API examples on GitHub:

sketchup-ruby-api-tutorials/examples at main · SketchUp/sketchup-ruby-api-tutorials · GitHub

The Sphere Tool should load when you restart SketchUp. If you do not want it to run, put the “ex_sphere_tool.rb” file into the sphere tool folder you created before restarting SketchUp.

You can modify code in NotePad++, save it, and restart SketchUp to test your changes.

If you want to use the Math methods without qualification, then you can can include Math into your extension submodule. Alex might have done this with his editor’s module.

I copy code from Notepad++ and paste it into SketchUp’s native Ruby Console all the time.

You can get an extension that helps you load and test code from development repositories:

1 Like

I found Console+ to be a more stable console tool when just playing around: Extension | SketchUp Extension Warehouse

1 Like

I find it useful @ene_su extension called Eneroth Script Runner * Eneroth Script Runner | SketchUp Extension Warehouse . I code with Visual Studio Code, save my file after changed it and then I drag the file to Sketchup. It’s fast and easy.

2 Likes