From: [email protected] [mailto:[email protected]]
#…
I just don’t understand one thing from the code above: the
“?\C-x”, in the when statement.
if you studied the code, you can deduce that it’s a literal character
for the keyboard combination Control-x. In ruby, single characters can
be represented by prefixing the ? to the char but without the usual
quoting.
There are some caveats however, and the best way to learn/test is using
irb, eg
RUBY_VERSION
#=> “1.8.6”
?\C-x
#=> 24
so in ruby1.8.6, the single char evaluates to an integer, wc if you look
closely, is the ascii code. again, note, it’s an integer.
?\C-x.class
#=> Fixnum
of course you can represent single chars as a string,
“\C-x”
#=> “\030”
oops, you’ll have to evaluate that further to get to the ? behaviour
030
#=> 24
ergo, you cannot compare ?-ied chars with quoted chars directly.
?\C-x == “\C-x”
#=> false
again,
?a
#=> 97
“a”
#=> “a”
?a == “a”
#=> false
also, judging fr the above, you cannot represent multi-byte chars using
the ? in ruby 1.8.6.
In ruby1.9 however, it’s much better and more natural. the way it should
be, so to speak.
?\C-x
=> “\x18”
“\C-x”
=> “\x18”
?a
=> “a”
“a”
=> “a”
?a == “a”
=> true
Ergo, in ruby1.9, you may not need the ? single char representation, the
usual quoting may do.
kind regards -botp