Is it acceptable to concatenate each and with_index? [duplicate] - ruby

This question already has answers here:
difference between each.with_index and each_with_index in Ruby?
(2 answers)
Closed 6 years ago.
Is it preferable not to concatenate each and with with_index like this
array.each_with_index { ... }
or is concatenation perfectly acceptable?
array.each.with_index { ... }

Both of these forms is correct and will work. In the case listed however, the first form is preferred as it uses the purpose built method for this task.
The second form should normally be reserved for methods that do not have a with index option as in this silly bit of code:
['a', 'b', 'c', 'd'].select.with_index {|_d, i| (i%2)==0}
which has as output:
["a", "c"]

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.

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] }

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.

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

Resources