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

**URL:** <https://www.ruby-forum.com/t/alternatives-to-c-n-m-a-etc-in-ruby-1-9/154277>\
**Category:** Ruby\
**Created:** [November 27, 2008, 7:53am UTC](https://www.ruby-forum.com/t/alternatives-to-c-n-m-a-etc-in-ruby-1-9/154277 "2008-11-27T07:53:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [November 27, 2008, 7:53am UTC](https://www.ruby-forum.com/t/alternatives-to-c-n-m-a-etc-in-ruby-1-9/154277/1 "2008-11-27T07:53:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [November 27, 2008, 8:20am UTC](https://www.ruby-forum.com/t/alternatives-to-c-n-m-a-etc-in-ruby-1-9/154277/2 "2008-11-27T08:20:30Z")

</div>

From: Nit K. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [November 27, 2008, 8:43am UTC](https://www.ruby-forum.com/t/alternatives-to-c-n-m-a-etc-in-ruby-1-9/154277/3 "2008-11-27T08:43:25Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [November 27, 2008, 8:49am UTC](https://www.ruby-forum.com/t/alternatives-to-c-n-m-a-etc-in-ruby-1-9/154277/4 "2008-11-27T08:49:47Z")

</div>

From: Nit K. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# \> 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

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [November 28, 2008, 3:12am UTC](https://www.ruby-forum.com/t/alternatives-to-c-n-m-a-etc-in-ruby-1-9/154277/5 "2008-11-28T03:12:09Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sentinel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@sentinel](https://www.ruby-forum.com/u/sentinel)\
**Post date:** [November 28, 2008, 6:31am UTC](https://www.ruby-forum.com/t/alternatives-to-c-n-m-a-etc-in-ruby-1-9/154277/6 "2008-11-28T06:31:35Z")

</div>

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](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

Thanks, yes, precisely what i wanted. 🙂
