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.
Related
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] }
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"]
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.
This question already has answers here:
Create two-dimensional arrays and access sub-arrays in Ruby
(9 answers)
Closed 9 years ago.
i created two dimensional array with the following code. but i can not store value in the array. row and columns of array grows dynamically. can not predict before.
arr = Array.new {Array.new}
and can not do this type of things.....
arr[0][0] = "Ruby"
You could create an abstraction over the standard array, like so
class MyArray < Array
def [](idx)
self.at(idx) ? self.at(idx) : self[idx] = []
end
end
Alternatively, you could use a Hash whose default_proc creates a new array at the specified index. OR a hash whose keys are [row, column]. This would be the best option for large datasets as your operations would be in O(1) time.
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