0

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]?

1
  • I'm using ruby 1.8.7 (2010-12-23 patchlevel 330) [i386-mingw32] Commented Apr 9, 2013 at 19:39

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I knew there had to be a better way!
0

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"

Comments

0

You can try the below:

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

5 Comments

I'm afraid that when I try this I get: NoMethodError: undefined method chr' for "AAATAAA":String
which version of ruby you are using. I am using Ruby 2.0.0
But I'm probably going to use Anthony Alberto's solution. If you look up in the comments I am using Ruby 1.8.7
@JoshD See here 1.8.7 not supported chr method.
That is weird since the .chr method works for me but I have: ruby 1.8.7 (2010-12-23 patchlevel 330) [i386-mingw32] maybe it's the patch level or something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.