Why are there slight different behaviors with array accesses? [duplicate] - ruby

This question already has answers here:
Array slicing in Ruby: explanation for illogical behaviour (taken from Rubykoans.com)
(10 answers)
Trying to understand Ruby arrays [duplicate]
(1 answer)
Closed 9 years ago.
My array is:
array = [:peanut, :butter, :and, :jelly]
array[4,0] gives []
But:
array[5,0] gives nil
I was suspecting array[4,0] should also give nil as the array has only a third index which is :jelly.
Can any Ruby punters out there clarify this behaviour?

It's all in the documentation:
Additionally, an empty array is returned when the starting index for an element range is at the end of the array.
Returns nil if the index (or starting index) are out of range.

Related

How to create an 2D array on an existing array in Ruby? [duplicate]

This question already has answers here:
How do I convert every element in an array to its own array?
(4 answers)
Closed 5 years ago.
Suppose I have an existing array here
["abc", "def"]
I wanted to create an array with the existing elements inside that array:
[["abc"], ["def"]]
Let's put every element into an array
a.map { |item| [item] }

Ruby n, 1 = gets.split.map &:to_i [duplicate]

This question already has an answer here:
Ruby x,y = gets.split.map &:to_i
(1 answer)
Closed 7 years ago.
I am looking at some ruby code.
But am unsure what is happening on this line:
n,1 = gets.split.map &:to_i
I understand some input array of strings is being mapped to an array of integers but why is that being assigned to n,1?
That line of code doesn't do anything, it's simply a SyntaxError. You can't assign to 1, that's not a legal name for a local variable.

Why a[3] returns nil, but a[3,0] returns [] in Ruby? [duplicate]

This question already has answers here:
Array slicing in Ruby: explanation for illogical behaviour (taken from Rubykoans.com)
(10 answers)
Slicing of arrays in ruby returns different result - nil vs. empty array
(1 answer)
Closed 8 years ago.
Given an array like this:
a = [1,2,3]
When I do a[3] it returns nil. And that makes sense because there is no more than 3 elements. However, when I do a slicing with a[3,0] or a[3,1], I get []
Why is that?

Ruby Array Indicies [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)
Running this code on my ruby interpreter it ends up with 7..4 outputting nil, and 6..4 outputting nothing
arr = [1, 2, 3, 4, 5, 6]
p arr[7..4]
p arr[6..4]
According to the ruby doc if the initial index is out of range it returns a nil, and in this case both (6 & 7) are out of range of the array, but only the first returns a nil.
Edit for clairification: The output is as follows:
nil
[]
Why would the first return nil and the second []?
Well, probably the interpreter doesn't understand the range "decreasing", trying
puts arr[3..1]
also returned nothing, maybe because [3..1] is not a range.
[]s

Ruby koan - about arrays - test_accessing_array_elements array[5,0] [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why does array.slice behave differently for (length, n)
In Ruby koan "about_arrays.rb", test_accessing_array_elements there's two similar start/length slice statements accessing parts of an array. Ref extract below. Both should be "out of range" yet one returns an empty array and the other returns nil. This matches possible results as per docs ruby doc. Why is it so?
irb(main):221:0> array = [:peanut, :butter, :and, :jelly]
=> [:peanut, :butter, :and, :jelly]
irb(main):222:0> array[4,0]
=> []
irb(main):223:0> array[5,0]
=> nil
irb(main):224:0>
irb(main):224:0> array[4]
=> nil
irb(main):225:0> array[5]
=> nil
irb(main):226:0>
I think of it as some array methods referring to the gaps between elements, rather than the elements themselves, i.e. 0 is the space before the first element, 1 the space between the first and second element. Considered this way, the operation makes slightly more sense, as 4 is the gap after the fourth element, which is still within the array, so zero elements from this position is an empty array.
You can think of the insert method in the same way (although the documentation explicitly says otherwise).
However, this is probably a mental trick rather than an explanation - still, perhaps it will help someone.

Resources