Iterators in Sketchup Ruby
-
Hello!
I'm new here, I'm new in Sketchup, I'm new in Ruby... But I'm not new in programming.
So, I have one maybe too stupid question.
I'm trying to use iterators like 'for in', until and .each. But they don't work! I think this is simpliest things in any programming language... and they work in all languages I use...
What's wrong with my Sketchup Ruby?
I use Sketchup 8 free version
-
It'd be much easier for us to understand - and reply - if you included actual text in the body of your post [copy+paste and use code-formatting tags].
Your screen-shots are not the most user friendly method...The iteration methods you desire are available in Ruby.
e.g. https://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators/
Most authors use them very frequently...To comment briefly on your issues...
(o..n-1).each.....
Where did that '
o
' come from?
I assume you meant '0
'Other similar methods are:
n.times.....
etc, read up on the Ruby iteration methods, there are many !
Also don't use capital letters to start a 'variable' [aka 'reference'] name.
Ruby assumes those are 'Constants' - they can still be reset, but then error messages appear in the Console.
Use lower-case characters - e.g.xxx
- to start normal references.
Capitals -XXX
- for Constants that do not change.
use@xxx
for references which are shared across methods in a Module , or those in an instance of a Class.
Use@@xxx
for references remembered across instances of a Class.
Note that$xxx
variables should be avoided, they apply globally, and are reserved to the core kernel use, and their use can always be replicated using @/@@ references anyway, limiting it to within your own 'name-space' module... -
Hi, TIG!
Thank you very much for your detailed answer, it is very usefull for me because I'm just a beginner in Ruby.
I'm sorry for bad pics. I post newBut... about this problem. I just copy code from this book
Automatic SketchUp Creating 3-D Models in Ruby by Matthew Scarpino 2010
It uses SketchUp 7.1 application programming interface (API)for count1 in 0..5 print count1, " " end
This code doesn't work in Sketchup 6, Sketchup 8, Sketchup 8 PRO. But it works in Sketchup 2015PRO...
-
The error message is saying that the interpreter parsed the text "for count1 in 0..5" and didn't find a line ending that it could understand. It wanted "do", ";", or "\n" (LF) and got something else. The fact that it works in 2015 suggests to me that this is an issue with the Ruby version embedded in SketchUp. Before SketchUp 2014 it was a variant of Ruby 1.8. As of 2014 it changed to Ruby 1.9 and since to successive versions of Ruby 2.x. One of the most important parts of the change was to require unicode character encoding. At least one of your screens suggests your computer locale is Russia? If so, your computer may be feeding unicode line endings that the older Ruby versions don't understand.
-
try a 'one liner' directly in SU's Ruby Console...
for count1 in 0..5; print count1, " "; end
I think the errors are caused by RubyConsolePro rather than the snippet...
on a PC, SU's Ruby Console has only recently accepted 'multiline' input...
Mat always saves the snippets as files and loads those...
john
Advertisement