Ruby Array Indicies [duplicate] - ruby

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

Related

How do you create your own multidimensional array? [duplicate]

This question already has answers here:
Ruby multidimensional array
(10 answers)
Closed 5 years ago.
I am coding and I came across this problem in my code, How do you create your own multidimensional array in Ruby? I tried searching up but none of the websites could help.
There are no multidimentional arrays in Ruby (at least they are not first class citizens.)
One might simulate this behavior by creating an array containing arrays of the same length:
arr = [[1, 2], [3, 4]]
or, dynamically:
arr = Array.new(2) { |_| Array.new(2) }
Also, there is Matrix class in standard library.

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?

How can I filter a hash from an array of symbols? [duplicate]

This question already has answers here:
Hash remove all except specific keys
(7 answers)
Closed 8 years ago.
So I have a hash that I would like to filter based on an array:
h = {a: 'test1', b: 'test2', c: 'test3'}
a = [:a, :poo1, :poo2]
My first thought was to try:
h.slice(a)
But that returns {}, when I hoped it would return {:a=>"test1"}. I can't seem to find a simple way of filtering my hash based on an array of symbols. Thoughts?
You can solve this by using the splat operator:
h.slice(*a)
This will produce
> {:a=>"test1"}
For an explanation what the asterisk does, please see this link.

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

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.

Resources