views:

265

answers:

2

How to I get the Fixnum returned by the following:

"abc"[2]

Back into a character?

+3  A: 

This will do it (if n is an integer):

n.chr
Greg Hewgill
+2  A: 

Be careful because Ruby 1.9 and later will return a single-character string for "abc"[2], which will not respond to the chr method. You could do this instead:

"abc"[2,1]

Be sure to read up on the powerful and multifaceted String#[] method.

nertzy