I have been investigating the string capabilities in depth,
and came across some issues with ascii conversions.
In other languages, chr() and ord() are functions that
convert them, as in:
chr(65) -> "A"
ord("A") -> 65
In Ruby, we use:
65.chr -> "A"
?A -> 65
Well, I was going to try to define a method String#ord, and
discovered that the '?' operator is purely a lexical thing
that looks at the character after it, so needless to say,
I had problems applying it to a string object.
Two issues follow from this.
1)
Is there some alternative to the following:
def ord
?(self[0].to_s)
end
obviously this doesn't work. Am I missing some other method?
2)
In IRB, I get some funny output with '?':
irb(main):016:0> ?A
65 # ok...
irb(main):017:0> ?\\
92
irb(main):018:0> ?\1
1
irb(main):019:0> ?\6
6
irb(main):020:0> ?\7
7
irb(main):021:0> ?\8
56 # first surprise
irb(main):022:0> ?\9
57
irb(main):023:0> ?\10
8 # second surprise, but makes sense
irb(main):024:0> ?\11
9
irb(main):025:0> ?\80
SyntaxError: compile error
(irb):25: parse error
(irb):25:in `irb_binding'
irb(main):026:0>
irb(main):027:0> ?\70
56 # same as ?\8
I suppose it is expecting octal values...right?
Any explanation for the surprises?
Thanks,
Guy N. Hurst
--
HurstLinks Web Development http://www.hurstlinks.com/
Norfolk, VA - (757)623-9688
PHP/MySQL - Ruby/Perl - HTML/Javascript