How start with ruby examples

I would like to try the ruby examples provided in exampleScripts.rb but it seems like the script doesn’t work.

First time I am using the Ruby console in Sketchup.

So I have some basic questions:

  • how to clear console
    - what does mean dolar sign in the command $exStrings = LanguageHandler.new("Examples.strings")
  • where is Examples defined?
  • why I did not see the “Box” in Draw menu when the menu should be added:
    UI.menu("Draw").add_item($exStrings.GetString("Box")) { create_box }
    I guess the script was not run.
  • Can I run the box script from console?
  • why this statement uses feet?
    values = [6.feet, 5.feet, 4.feet]
    [72.0, 60.0, 48.0]
    I am used to work with meters in my project. It is not clear why 6 feet is 72? What 72?
    If I change that comand to
    values = [6.m, 5.m, 4.m]
    [236.220472440945, 196.850393700787, 157.48031496063]
    What the hell is 236.220472440945? It’s not clear.
    -If I finally run the command
    UI.menu("Draw").add_item($exStrings.GetString("Box")) { create_box }
    And then I select Box from menu, I have the box created :slight_smile: But why I was not prompt to enter the values? It just created the cube but no inputbox was shown as in the example function. If I run it from the ruby console then it pops up dialog window to enter data.

cls :leftwards_arrow_with_hook:

$ is the prefix for a global variable. You should never use global variables.
They are for reading by everyone. Only the Ruby Core and the Trimble SketchUp Team should create global variables.

Instead, use class variables like @@var_name, or instance variables like @var_name, or local variables like var_name.

ALL of your code must be inside YOUR toplevel author module, and each of your plugins should be in a sub-module inside your toplevel module.

If you have SketchUo 2016, in the “su_examples” sub-folder of “Plugins”:
%AppData%/SketchUp/SketchUp 2016/SketchUp/Plugins/su_examples

The “%AppData%/SketchUp/SketchUp 2016/SketchUp/Plugins/su_examples.rb” file creates a SketchupExtension instance, that allows users to switch the extension loading On or OFF.

menu: Window > Preferences > Extensions

Highlight the “Ruby Script Examples” and check the box.

Yes. In the Console:

require "su_examples/box.rb"

… from then on, you call the command method:

Sketchup::Examples.create_box

Who knows? This example was written perhaps 15 years ago.
Maybe the default template (at that time) used “Engineering / feet” as the unit setting.

72 / 6 = 12
1 foot / 12 = 1 inch.

SketchUp use decimal inches internally. So most everywhere in the API your values will be entered in inches. The API created a special subclass Length (a subclass of Numeric) for this. See these classes:
class Length
class Numeric

First of all that was the correct thing to do, to make the example use meter units.

The method call .m() was added by the API to the Numeric class, in order to allow coders to specify lengths equal to metric units. The result of the .m() method call is an instance of the Length class (whose value is always in inches.)

There must be some other method named create_box() defined globally (at the toplevel which is inside Object.) This is bad. The method defined inside the Sketchup::Examples namespace modules is the one that needs to be executed.

I made a pinned post in the Ruby API category:

which lists:

Also you should read through the API FAQ pages and tutorials.
http://www.sketchup.com/intl/en/developer/docs/faq

Thanks for answers. I am using Sketchup 8 on Windows XP. The cls command did not worked, maybe you mean an icon in your App. I am learning Ruby, a lot things to read. I am not beginner programmer, but Ruby is new for me. I have tested the program Box in console and it works. Then I have activated the Ruby examples as you suggested me. It is late night so I will answer tomorrow.

Because it was not added until SketchUp 2014.

If you are using v8 Free, it would be better to also install SketchUp Make 2014.

Also it is difficult to use the Extension Warehouse and 3D Warehouse from v8.


In your forum profile, the SketchUp bitness will be 32-bit. (The 64-bit builds of SketchUp did not start until v2015, which do not run on XP.)


You’ll want to go here and read the one for the languages you already know:
:arrow_right: Ruby From Other [Programming] Languages

Unfortunately, SketchUp v2013 and earlier, use Ruby 1.8, which has many bugs and limitations. On PC it has problems with Unicode string support.

Ruby 2.x does not have these issues. Also it is faster than v 1.8.

Also SketchUp 2014 and later have come with the full Ruby Standard library. For earlier versions (7 thru 2013) you must install it separately:

My disk partition with Programs is full so I cannot install new programs right now. Probably I will solve this problem in future. How much space does the program need on disk c:? There is small place too so this is always problem when I want to install some modern program like Visual Studio or so…

When I learn ruby from Tutorialspoint there is such example:

   # ... process the file
aFile.close```

and another one
`val = gets`

both prints error in the Sketchup ruby console when I paste hem line by line. gets says it needs filename argument

    Error: #<Errno::EBADF: (eval):98:in `gets': Bad file descriptor>

    and the second code - I paste `aFile = File.new("filename", "mode")`

and the error is

    Error: #<ArgumentError: (eval):98:in `initialize': illegal access mode mode>
    (eval):98
    (eval):98:in `new'
    (eval):98

PS:
Ruby is weird language but it seems to me that it is very comfortable

create_box is defined in Examples/box.rb and this file is required from 'Examples/examplescripts.rb’
The examplescripts.rb is used by Plugins/examples.rb

Sketchup.register_extension examplesExtension, false```

so after I run the command `UI.menu("Draw").add_item($exStrings.GetString("Box")) { create_box }` 
I can to call the method create_box from menu.

Is it possible to download docs for Sketchup 8?

Edit:
Now finished reading your article about units in Sketchup. Thanks for it. Also for the code.

Thank @thomthom, it is his website.

Code posted in the forum must be wrapped in a code block, or the forum engine will swallow characters (especially error messages because they are within < and > symbols):

In-line code quotes must be surrounded with single backtick characters (`).

Multi-line code blocks begin and end with a line of 3 backtick characters (```).
At the end of the first line, can be a language name.

So this would be a ruby code block:

```ruby

# code goes here

```

It is hard to read your post above, you can edit them and fix the code. (Use the pencil icon to edit your posts.)

No. The API are online docs.
Each method has a doc property that says when it was added to the API.
You need to pay close attention to the API Release Notes, for mention of bugs fixed in later versions. (That way you’ll know what bugs you’ll deal with in v8.)

The Ruby Language v.1.8.6 docs are also online:
http://ruby-doc.org/core-1.8.6/index.html
http://ruby-doc.org/stdlib-1.8.6/


P.S. - I cannot teach you Ruby in a forum thread. Please read the books, and tutorials.
But keep in mind that embedded Ruby (inside an application) is a bit different than command line Ruby. Especially standard IO.

So inside SketchUp Ruby, use the block form of file access:

File.open("path/to/my/file.txt","r") {|file|
  one_line = file.gets
}

The File class is a subclass of class IO, so most instance methods you call on file objects, are defined in the IO superclass.
http://ruby-doc.org/core-1.8.6/File.html
http://ruby-doc.org/core-1.8.6/IO.html

When you type gets at the console, that is in the TOPLEVEL_BINDING, which means it is the global gets method, that is defined in module Kernel that gave you the error. That is the wrong method.

You need to call the file object’s gets method, that it inherited from class IO.
http://ruby-doc.org/core-1.8.6/IO.html#method-i-gets

Thank you much, this solves the problem. Now it is clear. I tried to correct the format of the code above. I also have a bit problem to search back in the current page because I am used to use ctrl+f to find a word or sentence in Firefox. Here it is reserved for different function which doesn’t suite my needs. If this feature could be turned off it would be better.