Iterators in Sketchup Ruby

Iterators in Sketchup Ruby

Postby Dragon3dgraff » Wed Dec 20, 2017 5:01 pm

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
0

Dragon3dgraff 
 

Re: Iterators in Sketchup Ruby

Postby TIG » Wed Dec 20, 2017 8:41 pm

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 ... iterators/
Most authors use them very frequently...

To comment briefly on your issues...
Code: Select all
(o..n-1).each.....
Where did that 'o' come from?
I assume you meant '0'

Other similar methods are:
Code: Select all
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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Iterators in Sketchup Ruby

Postby Dragon3dgraff » Thu Dec 21, 2017 12:25 pm

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 new

But... 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...
0

Dragon3dgraff 
 

Re: Iterators in Sketchup Ruby

Postby slbaumgartner » Fri Dec 22, 2017 4:07 pm

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.
0

slbaumgartner 
PluginStore Author
PluginStore Author
 

Re: Iterators in Sketchup Ruby

Postby driven » Fri Dec 22, 2017 5:01 pm

try a 'one liner' directly in SU's Ruby Console...

Code: Select all
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
2
learn from the mistakes of others, you may not live long enough to make them all yourself...

driven 
PluginStore Author
PluginStore Author
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

cron

Who is online

Users browsing this forum: No registered users and 16 guests

Visit our sponsors: