Purpose of Single Line Consoles for Ruby?
-
Hi, Chris here. First time caller, long time listener.
So SketchUp comes with a single line ruby console. I've always wondered what the purpose of it was. Jim Foltz released a multiline editor shortly after I began writing code and as soon as I tried it, I was hooked. I still use it every time I run SketchUp.
Since that time, Google also beta tested a "multiline" console, which is the exact one that has now been released in the trimble developer tools. It acts as a single line console unless you hold <ctrl><enter> then it allows you to enter more than one line of text. But as soon as you process the code, everything disappears and text entry box is reset to a single empty line.
So to me, this begs the question of what am I not understanding about the usefulness of single line editors? I don't even begin to comprehend why they exist for programming.
I was raised on DOS, a single-line command-line OS. That's easy for me to comprehend: change directory, delete file, open file, search, etc. But ruby is not an OS, its an object oriented language and to me it virtually always requires many lines of code to get test something.
Here's an example of something I typically use Jim's true multi line editor for:
Sketchup.active_model.selection.to_a.each do |e| < - 20 lines of code to test if e is a face, then get its vertices, then get all their transformations and push them all into an array of vertex transformations called vertices_array - > end puts vertices_array
And so by writing all that in a multi-line editor like Jim's, I can test it over and over until I'm sure I've got it all working smoothly and returning the array the way I think it should. But something like this is a nightmare in the single line editors that only sort of allow multi-line input. It's very difficult to write a
do .. end
statement that spans 20 lines in a console like the one from the developers tools.So that's the end of my rant, and I'm hoping someone has some sort of comprehension where I'm coming from and can help me understand the purpose of a single line console or any console that wants to evaluate line by line.
Thanks,
Chris
-
I think I can see that I'm conflating the concepts of editor vs. console. If so, I guess I only see the need for a built in simple editor, and I don't comprehend what a single line console is good for.
-
So, you're using it wrong!! LOL. Actually, you can use it however you want. Just because you are using it different
from me, doesn't make you wrong.Here's how I use it. I use it for one-line expressions, where I can watch the effects of my one-line expressions. For example, taking your example, I would distill your loop down to the fewest lines of code, and I would start entering them like...
sel = Sketchup.active_model.selection[0] ;
if (sel.is_a? Sketchup::Face) then ..... end ;To test the next item in the selection, I would recall the first command and change it to the next element
sel = Sketchup.active_model.selection[1] ;Then, I would recall the second command and press enter with no changes. Simple, simple. Minimal typing.
If I'm testing as many lines as you are, I'll create an isolated, specific "throw away" script to test that piece of logic, load it up via the console, (over and over as needed), and when I get it working, I'll paste it into my "proper" script.
Todd
-
@chris fullmer said:
Jim Foltz released a multiline editor shortly after I began writing code and as soon as I tried it, I was hooked. I still use it every time I run SketchUp.
Ah yes, a classic.
I posted it on GitHub. It still works after all this time. I'm satisfied with the basic set of features, but it could use a facelift.
-
IMO this one-liner is excellent for entering the
load 'myscript.rb'
command. Where myscript.rb is edited with a synax highlighting multiline text editor. -
@gĂĄbor said:
IMO this one-liner is excellent for entering the
load 'myscript.rb'
command. Where myscript.rb is edited with a synax highlighting multiline text editor.This is how I use the console. A multi-line console is not needed nor is it preferred, IMO. The vast majority of development that I do is in a high-end programmer's editor. All I need to do is load the file in the Ruby console. The history capability mentioned by Todd is also handy in the one-line console.
-
Very interesting. I also use it (and value it) for being able to
load 'myscript.rb'
.Todd, I'm sure I'm using it wrong so its good to see how its actually useful in some scenario other than running the occasional 'load' command. I think I've never trained my brain to think of code as one-liners that progress from line to line, so its interesting to see how you would process similar code.
At least this begins to help my mind release some of the anxiety I get over the single line consoles.
I'm considering trying the method of writing in a test file and loading that file over and over. Maybe I'll like that process.
Thanks everyone,
Chris
-
Here's your one-liner:
UI.toolbar("Reload").add_item(UI::Command.new("Reload"){load "myplugin.rb"}).show
- This is a bad idea creating temporary toolbars that will actually stay in the Registry and eventually slow down SketchUp.
-
@jim said:
Here's your one-liner:
UI.toolbar("Reload").add_item(UI::Command.new("Reload"){load "myplugin.rb"}).show
Thank you for the improved version.
-
@chris fullmer said:
I think I can see that I'm conflating the concepts of editor vs. console. If so, I guess I only see the need for a built in simple editor, and I don't comprehend what a single line console is good for.
I think this is the key here. You use it to develop plugins / snippets directly into SketchUp. (Which makes sense as you get a direct result.)
Personally I'm too paranoid that SketchUp crashes and bye-bye goes all my work. And I have grown to rely heavily on the features Notepad++ or Sublime2 gives me that I always write my code in an editor.I use the console for debug output, reloading, and minor testing (multiple command over multiple lines, or multi-command-one-liners.)
(Btw, isn't it Shift+Return for the new-line in the SU developer console?)
Advertisement