How can I get Ruby to treat the index of a string as a character (rather than the ASCII code)?

Posted by user336777 on Stack Overflow See other posts from Stack Overflow or by user336777
Published on 2010-05-09T19:28:17Z Indexed on 2010/05/09 19:38 UTC
Read the original article Hit count: 139

Filed under:
|
|

I am checking to see if the last character in a directory path is a '/'. How do you get ruby to treat the specific index of a string as a character rather than the associated ASCII code?

For example the following always returns false:

dir[dir.length - 1] == '/'

This is because dir[dir.length - 1] returns the ASCII code 47 (rather than '/').

Any thoughts on how to interpret 47 as '/'? Or is there a completely different way to handle this in the first place?

thanks.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about strings