Ruby 1.9: handling meta and control keys

1.8 used to return an int for say ?\M-a or ?C-a (when user types meta-a
or control-a).

1.9 returns a string as “\xE1” instead of 225.

The only way i have been able to convert “\xE1” to 225 is removing the
first 2 chars and then using to_i(16).

“E1”.to_i(16)

If i do:

“\xE1”.to_i(16) i get a zero.

Is there a clean way of handling meta and control chars in 1.9. Or at
least a clean way of converting them to integers. I need them as
integers since I am porting a 1.8 app that uses them as integers.

Also, is there anyone using the return values of ?\M-a etc as is,
without conversion. I would like to hear how.

(rkumar) Sentinel wrote:

1.8 used to return an int for say ?\M-a or ?C-a (when user types meta-a
or control-a).

1.9 returns a string as “\xE1” instead of 225.

After further searching on :
http://blog.grayproductions.net/articles/what_is_a_character_encoding

I found this:

?\M-a.getbyte(0)

It works, however any suggestions on how i can write this so it works
for both 1.8 and 1.9 would be welcome.

On Oct 4, 2009, at 00:28 , (rkumar) Sentinel wrote:

I found this:

?\M-a.getbyte(0)

It works, however any suggestions on how i can write this so it works
for both 1.8 and 1.9 would be welcome.

This is what I use in ruby_parser:

I hate ruby 1.9 string changes

class Fixnum
def ord
self
end
end unless “a”[0] == “a”

Ryan D. wrote:

class Fixnum
def ord
self
end
end unless “a”[0] == “a”

Thanks for the instant reply. I’ve followed the same with getbyte.
– rk

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs