I read in another answer that the []
method can take an array as an argument.
Both the examples given in the linked post don't illustrate what the actual result is from using this technique.
I tried some examples which also don't provide much information:
[11] pry(main)> a = %w( foo bar baz )
=> ["foo", "bar", "baz"]
[12] pry(main)> a[[1,2]]
TypeError: no implicit conversion of Array into Integer
from (pry):11:in '[]'
[13] pry(main)> a[['foo', 'bar']]
TypeError: no implicit conversion of Array into Integer
from (pry):12:in '[]'
[14] pry(main)> b = { foo: 42, bar: "dolphins", baz: "towels" }
=> {:foo=>42, :bar=>"dolphins", :baz=>"towels"}
[15] pry(main)> b[[:foo, :bar]]
=> nil
[16] pry(main)> b[["dolphins"]]
=> nil
What does it mean for the []
to take an array as an argument? What context is this technique used?
Would appreciate some runnable examples that will help me understand why all my examples return either nil
or an error.