Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3403 > unrolled thread
| Started by | Duke Normandin <dukeofperl@ml1.net> |
|---|---|
| First post | 2011-04-23 11:22 -0500 |
| Last post | 2011-04-23 13:12 -0500 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.ruby
Array indexing Duke Normandin <dukeofperl@ml1.net> - 2011-04-23 11:22 -0500
Re: Array indexing 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-23 12:02 -0500
Re: Array indexing Josh Cheek <josh.cheek@gmail.com> - 2011-04-23 12:37 -0500
Re: Array indexing Duke Normandin <dukeofperl@ml1.net> - 2011-04-23 13:12 -0500
| From | Duke Normandin <dukeofperl@ml1.net> |
|---|---|
| Date | 2011-04-23 11:22 -0500 |
| Subject | Array indexing |
| Message-ID | <alpine.DEB.2.00.1104231013590.6836@fryrpg-zna> |
hey ... I want to make sure that I understand the subtle difference in the syntax. my_array[0,3] and my_array[0..2] both will return the same array elements - correct? the first _says_: "start at index0 & gimme 3" the latter _say_: "start at index0 and goto index2" If the data requirements start at >index0, then there is no offset between the "gimme" and the 'goto". Am I getting it? -- Duke
[toc] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-23 12:02 -0500 |
| Message-ID | <d8620da7d932531bdc3de6583e33ab79@ruby-forum.com> |
| In reply to | #3403 |
Duke Normandin wrote in post #994657: > hey ... > > I want to make sure that I understand the subtle difference in the > syntax. > > my_array[0,3] > > and > > my_array[0..2] > > both will return the same array elements - correct? > If someone had a gun to your head and told you that you had 30 seconds to provide an answer to that question, what would you do? > the first _says_: "start at index0 & gimme 3" > the latter _say_: "start at index0 and goto index2" > Where is the ruby documentation located? > If the data requirements start at >index0, then there is no offset > between the "gimme" and the 'goto". > > Am I getting it? No. -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Josh Cheek <josh.cheek@gmail.com> |
|---|---|
| Date | 2011-04-23 12:37 -0500 |
| Message-ID | <BANLkTikV+YNXmhKHkpRAVggCgNwLp-GuXw@mail.gmail.com> |
| In reply to | #3403 |
[Note: parts of this message were removed to make it a legal post.]
On Sat, Apr 23, 2011 at 11:22 AM, Duke Normandin <dukeofperl@ml1.net> wrote:
> hey ...
>
> I want to make sure that I understand the subtle difference in the
> syntax.
>
> my_array[0,3]
>
> and
>
> my_array[0..2]
>
> both will return the same array elements - correct?
>
> the first _says_: "start at index0 & gimme 3"
> the latter _say_: "start at index0 and goto index2"
>
> If the data requirements start at >index0, then there is no offset
> between the "gimme" and the 'goto".
>
> Am I getting it?
> --
> Duke
>
>
IDK, but a quick study of behaviour should make it very obvious:
numbers = *0...10 # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers[0,3] # => [0, 1, 2]
numbers[1,3] # => [1, 2, 3]
numbers[2,3] # => [2, 3, 4]
numbers[3,3] # => [3, 4, 5]
numbers[0,4] # => [0, 1, 2, 3]
numbers[1,4] # => [1, 2, 3, 4]
numbers[2,4] # => [2, 3, 4, 5]
numbers[3,4] # => [3, 4, 5, 6]
numbers[0..5] # => [0, 1, 2, 3, 4, 5]
numbers[1..5] # => [1, 2, 3, 4, 5]
numbers[2..5] # => [2, 3, 4, 5]
numbers[3..5] # => [3, 4, 5]
numbers[3..7] # => [3, 4, 5, 6, 7]
numbers[4..7] # => [4, 5, 6, 7]
numbers[5..7] # => [5, 6, 7]
numbers[6..7] # => [6, 7]
numbers[7..7] # => [7]
If you're using TextMate, you can update the comments with
command+control+shift+e. Otherwise, you might try entering each line into
irb, and it will update for you the correct value. Or you could write a
program to cycle through inputs and print the values, something like:
numbers = *0...10
(0..3).each do |start|
result = numbers[start,3]
puts "numbers[#{start},3] # => #{result.inspect}"
end
On Sat, Apr 23, 2011 at 12:02 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
>
> If someone had a gun to your head and told you that you had 30 seconds
> to provide an answer to that question, what would you do?
>
>
Ruby is serious business.
[toc] | [prev] | [next] | [standalone]
| From | Duke Normandin <dukeofperl@ml1.net> |
|---|---|
| Date | 2011-04-23 13:12 -0500 |
| Message-ID | <alpine.DEB.2.00.1104231207050.6836@fryrpg-zna> |
| In reply to | #3409 |
On Sun, 24 Apr 2011, Josh Cheek wrote: > On Sat, Apr 23, 2011 at 11:22 AM, Duke Normandin <dukeofperl@ml1.net> wrote: > > > hey ... > > > > I want to make sure that I understand the subtle difference in the > > syntax. [snip] > > > IDK, but a quick study of behaviour should make it very obvious: > > [snip the good stuff] > Thanks for _all_ that! However, I think that I summarized it in my original post, exactly as you are showing it above. My definition of "gimme" and "goto" may not be as clear as what I had hoped it would be. No matter! I'll check my understanding of the issue against what you've provided above. Much obliged! -- Duke
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web