I am trying to find the first character of a string inside an array. I would like to do something like this:
string = ["A", "B", 1234, 54321]
string[3].chars.first # => "5"
Doing "string".chars.first # => "s" only works for a string input.
You could change all of the elements of the array to strings then do what you were originally doing.
string = ["A", "B", 1234, 54321]
string.map { |x| x.to_s }[3].chars.first
=> "5"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With