Peter Coulton
Peter Coulton

Reputation: 55789

How do I convert a character code back to a character?

How to I get the Fixnum returned by the following:

"abc"[2] 

Back into a character?

Upvotes: 8

Views: 6489

Answers (2)

Grant Hutchins
Grant Hutchins

Reputation: 4409

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.

Upvotes: 4

Greg Hewgill
Greg Hewgill

Reputation: 992897

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

n.chr

Upvotes: 15

Related Questions