Suppose I have this:
x = %w(greater yellow bandicooot)
And I want to get a specific letter of each string as a string. Of course, I can do something like this (to get the first letter):
x.map { |w| w[0] }.join # => 'gyb'
But I'd like to know whether or not there's a way to do it using just array notation. I've tried this:
x[0][0]..x[-1][0]
Which returns, in this case, the not-so-helpful "g".."b"
. I could also use array notation like this in this case:
x[0][0] + x[1][0] + x[2][0]
But I'm looking for a non-case-specific solution that doesn't require iteration.
Is there a way to do this strictly with array notation, or is it necessary to do some sort of iteration? And if you can't do it with array notation, is there a better way to do it than using map
and join
?