Lifeweaver
Lifeweaver

Reputation: 1042

Array returning Fixnum instead of String

I have an array of arrays similar to:

[["Rosalind_0498", "AAATAAA"], ["Rosalind_2391", "AAATTTT"]]

I I want to get the letter of the first array's second element I would expect to use array[0][1][-1]

This instead of returning 'A' returns 65, it's probably something simple to do with ruby arrays but I'm not sure why it is happening, can someone point me in the right direction?

UPDATE:

Is there a better way to do it then array[0][1][-1..-1]?

Upvotes: 0

Views: 148

Answers (3)

Arup Rakshit
Arup Rakshit

Reputation: 118261

You can try the below:

p [["Rosalind_0498", "AAATAAA"], ["Rosalind_2391", "AAATTTT"]].flatten[1].chr #=> "A"

Upvotes: 0

Anthony Alberto
Anthony Alberto

Reputation: 10395

Prior to Ruby 1.9, accessing a string char with [] would get you the ascii value of this char.

Just use this in ruby 1.8:

array[0][1][-1].chr

Upvotes: 3

ennuikiller
ennuikiller

Reputation: 46965

what version of ruby are you using?

2.0.0p0 :001 > a = [["Rosalind_0498", "AAATAAA"], ["Rosalind_2391", "AAATTTT"]]
 => [["Rosalind_0498", "AAATAAA"], ["Rosalind_2391", "AAATTTT"]]
2.0.0p0 :002 > a[0][1][0]
 => "A"

Upvotes: 0

Related Questions