I would like to load a bunch of instructions to the ruby console from a file and then continue extending them in interactive mode.
However, if is “load” the file, it executes and then the context disappears. For example all the variables are gone and if I try to use them in interactive mode on the console it doesn’t have them.
I hope my question is clear.
Can anyone tell me how I can solve this?
You just need a bit more of experience, and you will get it by using the Ruby Console. The Ruby Console is for interactive use.
A Ruby file contains definitions of methods (def methodname
…) and maybe also commands to invoke methods. When a Ruby file is loaded, all such executable code is run. Usually it makes sense to have a method call that registers a menu item with a method definition (so that a menu item is created to run your script), but not to invoke the method directly, because then it is already run when the script is loaded (and maybe the model is not ready for the method to run).
The life of a variable depends on the scope of the variable, the context in which it is allowed to live. When you define variables in the Ruby console, they are usually global (local) variables. Variables inside methods or classes are not so easy to access, and there are good reasons for that (encapsulation).
It would be best if you attach some example of what you are trying to achieve.
Part of your problem could be if you are envisioning the SketchUp Ruby Console as a source code editor, which it is not. It is simply an execution environment in which you can enter Ruby code and have it executed at once. “Enter” includes issuing load commands that read in and execute the contents of a file. The consequences of executing are retained, e.g. class and function defs and side-effects such as drawing on the view, but the source code is not. You can review and revise what you previously typed by means of the up arrow, but you cannot edit the contents of a source file. For that you need either an external editor or an extension that supports editing within SketchUp (I believe Alex Schreyer wrote one?).
If you are looking for editing files and re-executing them, see here Alex’ Ruby Code Editor.
Thanks for the reply.
I will give a trivial example.
I have a file (“Test.rb”) with the following code:
mod=Sketchup.active_model #Open model
ents=mod.entities #all entities in the model
pt[0]=[0,0,0]
pt[1]=[0,1,0]
pt[2]=[1,1,0]
pt[3]=[1,0,0]
I am using the console and I do the following:
Load “Test.rb”
Then, from the console I want to type:
rect=ents.add_face pt
However, when I do this, the console doesn’t know anything about pt[0] etc.
If I had typed all those lines in the console it would, but when I do it from a file it doesn’t.
Is there a way to solve that?
Huck
Can you (please) edit your post and mark code as code – that’s what the </> button stands for.
(Also take care that some software has replaced straight quotes by typographic quotes, which don’t eval in the Ruby Console).
Assuming pt = []
was also defined in your file, (and load
in lowercase), I believe the reason is that Ruby does a garbage collection after loading the file. As said above, these variables have local scope (although at top level). When local variables are not needed anymore, they are cleaned up and disappear. This is normally not a problem, because local variables are supposed to be used only in methods, and not at all at top level.
A solution would be to use persistent variables (module or class variables), but they are not so easy to access (information hiding).
module Huck
@pt = [ [0,0,0], [0,1,0], [1,1,0], [1,0,0] ]
end
A simpler trick is to read your file as string and evaluate it. (But it uses the evil eval
which one should not use in published plugins if avoidable.)
eval(File.read("Test.rb"))
Thanks.
I think the “eval” will do what I want. I’ll have to play around with it a bit to see. Of course I would not use that evil item in a plugin. It’s just for me to play with code and test little pieces.
I’m quite new to this and I’m constantly making errors of using lower case or upper case when I shouldn’t (I think I would criticize Ruby for that. I preferred declaring variables which I think I did in Pascal, but it’s been so long I’m not really sure.)
The interactive mode helps me learn.
Thanks again.
Huck