Alternatives to ?\C-n, ?\M-a etc in ruby 1.9

I need to make a rather important design in my 1.8 application, so i
don’t have too much rework to do when i want to port to 1.9.

I understand that in 1.9 we would use String.ord for cases like a[0] or
?a (getting ascii value).

But what about ?\C-a or ?\M-a (control and meta keys). Is there a
workaround for them?
Sorry, I don’t have 1.9, so cant try out “\C-a”.ord .

Also,
To be compatible with both 1.8 amd 1.9, would i use something like:
ch = str.respond_to? :ord ? str[0].ord : str[0]
or
str[0] is_a? Fixnum ? str[0] : str[0].ord
Anyone else doing such things?
Thanks.

From: Nit K. [mailto:[email protected]]

But what about ?\C-a or ?\M-a (control and meta keys).

Is there a workaround for them?

Sorry, I don’t have 1.9, so cant try out “\C-a”.ord .

am on a win machine, so my ruby1.9 is old ver. sorry.
anyway, ctl and meta still work.

eg,

C:\family\ruby>\ruby1.9\bin\ruby -ve ‘p “\C-m”; p “\r”’
ruby 1.9.0 (2008-06-20 revision 17482) [i386-mswin32]
“\r”
“\r”

C:\family\ruby>\ruby1.9\bin\ruby -ve ‘p “\C-m”.ord; p “\r”.ord’
ruby 1.9.0 (2008-06-20 revision 17482) [i386-mswin32]
13
13

Also, To be compatible with both 1.8 amd 1.9, would i

use something like:

ch = str.respond_to? :ord ? str[0].ord : str[0]

or

str[0] is_a? Fixnum ? str[0] : str[0].ord

why not create your own String#ord func?

i think string#ord works for both 1.8.7 and 1.9, so you just have to
create one for 1.8.6

why not create your own String#ord func?

i think string#ord works for both 1.8.7 and 1.9, so you just have to
create one for 1.8.6
I am on 1.8.7:

irb(main):334:0> “a”.ord
NoMethodError: undefined method `ord’ for “a”:String
from (irb):334

From: Nit K. [mailto:[email protected]]

> why not create your own String#ord func?

>

> i think string#ord works for both 1.8.7 and 1.9, so you

just have to

> create one for 1.8.6

I am on 1.8.7:

irb(main):334:0> “a”.ord

NoMethodError: undefined method `ord’ for “a”:String

from (irb):334

oops, sorry, you’re right, the #ord in 1.8.7 works only for integer…

qri ord

Returns the int itself.

    ?a.ord    #=> 97

This method is intended for compatibility to character constant in Ruby
1.9. For example, ?a.ord returns 97 both in 1.8 and 1.9.

i guess you’ll really have to create one for 1.8.x

On Nov 27, 2008, at 2:44 AM, Peña, Botp wrote:

from (irb):334

Ruby 1.9. For example, ?a.ord returns 97 both in 1.8 and 1.9.

i guess you’ll really have to create one for 1.8.x

IIRC, This came originally from Jim W.:

class String

Future-proof by adding an #ord method to return the integral

value of a

string (the first character).

unless instance_methods.include?(‘ord’) # Ruby 2.0
def ord
unless size == 1
raise TypeError, “expected a characer, but string of size %ld
given” % size
end

   self[0]                   # Ruby 1.8
 end

end

end

I think that’s what you’re looking for.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Rob B. wrote:

IIRC, This came originally from Jim W.:

I think that’s what you’re looking for.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Thanks, yes, precisely what i wanted. :slight_smile: