Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the first character in an array string

Tags:

arrays

ruby

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.

like image 697
Jacob Wanner Avatar asked Dec 21 '25 13:12

Jacob Wanner


1 Answers

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"
like image 110
Elijah Avatar answered Dec 24 '25 03:12

Elijah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!