Can someone explain

I am trying to following along with the information on this website
SketchUp script clips #2: Creating geometry – by [as] (alexschreyer.net)

in the first script he used this code

(0..n-1).each { |i|

in the next scrip[t he used something a little different

(0..n).each { |i|

I do not understand the difference between the n and the n-1
they seemed to do the same thing when I changed them out

also I could not figure what the |i| was

sorry I messed up my use of the tilde

also after the 3rd script he said this

In addition, it may be possible that you need to add the Math class to the “sin” function. It works fine for me as shown but the long version would be:
Math.sin(x)

Since that script will not work for me, I thought this statement might apply. But I do not understand. In another post I was told by DanRathbun

If you want to use the Math methods without qualification, then you can can include Math into your extension submodule. Alex might have done this with his editor’s module.

Sorry cannot figure how to share quotes between posts

How do I include Math into my extension submodule

You highlight (select) the text and a Quote button will appear.

The parameter list for a block is delimited by the "|" (pipe) character.

In the case of an iterator method the object from the collection is passed into the block.
You can use any variable name that you choose. Alex chose to use i.
(Often i is used for an index variable, but in this case the collection is a set of integers, so this fits as well.) You also use the name num or int. Doesn’t matter, whatever your style is.

1 Like

It’s not rocket science. If you look up the include method in the Core Ruby docs you’ll see it straight forward simple.

module BooneyRex
  module SomeExtension

    include Math

    # ... other code ...

  end
end
1 Like

Iterators

To avoid “fence post errors” try to use Ruby’s better iterators:

n.times {|i| puts i }

or

n.times do |i|
  puts i
end

Or assign a reference to a collection:

faces = ents.grep(Sketchup::Face)

faces.each do |face|
  puts Sketchup.format_area(face.area)
end

… also …

for face in faces
   puts Sketchup.format_area(face.area)
end 

The difference between n and n-1 is that n is 1 more than n-1 :wink:

Usually in order to not do something more than n times, coders will create a Range as (0..n-1), but they could also do (1..n) for the Range.

Or instead of using two dots to define the Range as in (0..n-1), they can do it with 3 dots and the last number is omitted, as in (0...n). (So check Alex’s examples again and note whether 2 dots or 3 dots were used.)

1 Like

@DanRathbun didn’t make clear that when you have an Array of n objects, their indices in the Array run from 0 to n-1. If you try to read from a[n] you will not get a valid element. That’s why Ruby has the two-dot and three-dot variants, so you can choose according to what your code really needs. As Dan pointed out, the two vs three distinction can be confusing, and alternatives may be clearer and safer.

2 Likes

for some reason that did not work for me this time. Maybe a glitch in the computer today.

Thank you. I did not realize that is what that was talking about.

:black_cat::black_cat:

1 Like