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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0026dd77fd9ecc97b36e5b79cdbcf590?d=identicon&s=25 R. Kumar (sentinel)
on 2008-11-27 07:53
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.
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-11-27 08:20
(Received via mailing list)
From: Nit Khair [mailto:sentinel.2001@gmx.com]
# 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
0026dd77fd9ecc97b36e5b79cdbcf590?d=identicon&s=25 R. Kumar (sentinel)
on 2008-11-27 08:43
>
> 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
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-11-27 08:49
(Received via mailing list)
From: Nit Khair [mailto:sentinel.2001@gmx.com]
# > 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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-11-28 03:12
(Received via mailing list)
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 Weirich:

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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
0026dd77fd9ecc97b36e5b79cdbcf590?d=identicon&s=25 R. Kumar (sentinel)
on 2008-11-28 06:31
Rob Biedenharn wrote:
> IIRC, This came originally from Jim Weirich:
>
>
> I think that's what you're looking for.
>
> -Rob
>
> Rob Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com

Thanks, yes, precisely what i wanted. :-)
This topic is locked and can not be replied to.