I am not on a device where I can look at the code, but look for an eval() statement. You can bind the evaluation to the top-level scope by providing a second argument of TOP_LEVEL to the eval method.
EDIT: The constant is actually TOPLEVEL_BINDING as noted below by Dan.
He did this purposely to prevent modification of API modules and classes like you attempted, and Ruby core base classes.
Close. But the global constant is TOPLEVEL_BINDING.
Fran, keep in mind that the binding of the toplevel, is a special instance of Object and so any local or instance vars or constants will be global and propagate into everything. (This can upset other coders.)
Normally, Ruby has a special evaluation for scripts that wraps an anonymous module around the script code to prevent the persistence of local variables in the global objectspace. If you are pasting code into the console, be sure all of it is wrapped within your toplevel author/company namespace module. (Just because other people’s examples are wrong doesn’t mean you should follow poor practice.)
Now, all this aside, I have Andreas’ Ruby Console+ installed and it begins, by default within the TOPLEVEL_BINDING which he calls “global”.
The control is a simple text inputbox in the center of the captionbar.
@Yoni - you have run into one limitation of writing code using the (any) browser-based Code Editor - you will soon run into others. They may seem like a good idea, do not scale well for code other than testing single functions.
Use a good text editor with Ruby language support to write your code. Notepad++ (Windows) is good if you don’t have a preference. Then as John says switch to SketchUp and re-load your file to execute it.
It’s easy to create a one-time menu for loading a file. Then you can set a shortcut and then it becomes very fast to execute it.
I understand the risks involved… it’s for quick and easy tests. Also, the intention is to surround my code within my own modules, and if possible, to see how it works directly from the Ruby Code Editor… without copying and pasting endlessly from one place to anthoer…
@DanRathbun , the “anonymous module around the script code”, in what situations is that done?
If I were to run the code as though it were in the TOPLEVEL_BINDING, will it work exactly the same as if it’s being loaded from the plugins folder!?
When I reference to file locations using FILE , I believe it reacts differently depending on in which console I’m running the code from… or which folder the file is located at… I understand that, but I’m wondering what would be the best way to test run code as close as possible to the end product… (in the plugins folder).
In the mean time I started working now like you guys suggested, with an outside editor - I find “Visual Studio Code” to be nice and comfortable (though a bit slow to start) and copying and pasting snippets to Ruby Console+ or directly to the Ruby Console…
It’s a bit of a nag, cause I’m constantly testing little trivial changes until I understand what the * I’m doing… but it’s not too bad I guess.
Using the ‘Sketchup Plugins Manager’ I’ve made a preset with a minimal set of plugins loaded, so reloading Sketchup is a lot quicker now… very helpful.
Visual Studio Code is an excellent editor even though Ruby Support is a little weak.
You don’t need to do this. After you have saved your changes to the code in VSCode, Alt-Tab to SketchUp, select the Ruby Console, and reload the .rb file. If you have methods defined they will be updated to the most recently loaded version.
For even faster loading, you can make a quick menu to reload your code and optionally execute a method:
UI.menu.add_item("Reload My File") { load("test/code.rb"); main_method() }
I just found this oh so very relevant extension… which @jim_foltz happened to write!
I think it’s a good idea to mention it here in association with the contents of this thread.
So helpful!!! http://sketchucation.com/plugin/1632-ruby_toolbar
You can modify modules, classes, methods, etc., at runtime, or anytime.
There are also some dedicated methods for undefining methods and constants and classes, specifically.