CommonThread

Tip: Ruby Range Operators

Posted by anthony crumley, Fri Nov 23 08:10:00 UTC 2007

There are actually two Ruby range operators. The .. operator is inclusive of the end points and the … operator is exclusive of the high value end point.

a = [0, 1, 2, 3]

a[0..-1] => [0, 1, 2, 3]
a[0...-1] => [0, 1, 2]

You would think … would be helpful for using a.length for the high end point but it doesn’t seem to matter.

a[0..a.length] => [0, 1, 2, 3]
a[0...a.length] => [0, 1, 2, 3]

Filed Under: | Tags:

Comments

Have your say

A name is required. You may use HTML in your comments.




Categories