Shpigford
Shpigford

Reputation: 25378

How to select the first inner array?

I have the following:

arr = [["1/31/2012 8:00 PM"]]

Right now, to get that string I end up doing arr.first.first...which just seems awkward.

What's a more direct way to get 1/31/2012 8:00 PM?

Upvotes: 1

Views: 94

Answers (2)

rubyprince
rubyprince

Reputation: 17803

Your code seems the correct way to do it, but, you can also do

arr.to_s
=> "1/31/2012 8:00 PM"

But be cautious. This will concatenate the elements in the array into a single string, if more than one element is present in the array

[["this", "is"]].to_s
=> "thisis"

[["this", "is"], ["test"]].to_s
=> "thisistest"

Upvotes: 0

Aliaksei Kliuchnikau
Aliaksei Kliuchnikau

Reputation: 13739

It depends on your array purpose, you have several options:

  1. arr[0][0] is is equal to arr.first.first. But I think arr.first.first is normal solution
  2. arr.flatten.first
  3. Consider other structure for arr with which you will be able to query for this data more naturally like meeting.nearest # => "1/31/2012 8:00 PM".

Upvotes: 4

Related Questions