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

N

Nit Khair

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? :eek:rd ? str[0].ord : str[0]
or
str[0] is_a? Fixnum ? str[0] : str[0].ord
Anyone else doing such things?
Thanks.
 
N

Nit Khair

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
 
P

Peña, Botp

RnJvbTogTml0IEtoYWlyIFttYWlsdG86c2VudGluZWwuMjAwMUBnbXguY29tXSANCiMgPiB3aHkg
bm90IGNyZWF0ZSB5b3VyIG93biBTdHJpbmcjb3JkIGZ1bmM/DQojID4gDQojID4gaSB0aGluayBz
dHJpbmcjb3JkIHdvcmtzIGZvciBib3RoIDEuOC43IGFuZCAxLjksIHNvIHlvdSANCiMganVzdCBo
YXZlIHRvIA0KIyA+IGNyZWF0ZSBvbmUgZm9yIDEuOC42DQojIEkgKmFtKiBvbiAxLjguNzoNCiMg
DQojIGlyYihtYWluKTozMzQ6MD4gImEiLm9yZA0KIyBOb01ldGhvZEVycm9yOiB1bmRlZmluZWQg
bWV0aG9kIGBvcmQnIGZvciAiYSI6U3RyaW5nDQojICAgICAgICAgZnJvbSAoaXJiKTozMzQNCg0K
b29wcywgc29ycnksIHlvdSdyZSByaWdodCwgdGhlICNvcmQgaW4gMS44Ljcgd29ya3Mgb25seSBm
b3IgaW50ZWdlci4uDQoNCj5xcmkgb3JkDQogICAgIC4uLg0KICAgICBSZXR1cm5zIHRoZSBpbnQg
aXRzZWxmLg0KDQogICAgICAgID9hLm9yZCAgICAjPT4gOTcNCg0KVGhpcyBtZXRob2QgaXMgaW50
ZW5kZWQgZm9yIGNvbXBhdGliaWxpdHkgdG8gY2hhcmFjdGVyIGNvbnN0YW50IGluIFJ1YnkgMS45
LiBGb3IgZXhhbXBsZSwgP2Eub3JkIHJldHVybnMgOTcgYm90aCBpbiAxLjggYW5kIDEuOS4NCg0K
aSBndWVzcyB5b3UnbGwgcmVhbGx5IGhhdmUgdG8gY3JlYXRlIG9uZSBmb3IgMS44LngNCg==
 
R

Rob Biedenharn

From: Nit Khair [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..
...
Returns the int itself.

?a.ord #=3D> 97

This method is intended for compatibility to character constant in =20
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 =20
value of a
# string (the first character).
unless instance_methods.include?('ord') # Ruby 2.0
def ord
unless size =3D=3D 1
raise TypeError, "expected a characer, but string of size %ld =20=

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top