Getting an Ascii Character Code in Ruby Using '' (Question Mark) Fails

Getting an ASCII character code in Ruby using `?` (question mark) fails

Ruby before 1.9 treated characters somewhat inconsistently. ?a and "a"[0] would return an integer representing the character's ASCII value (which was usually not the behavior people were looking for), but in practical use characters would normally be represented by a one-character string. In Ruby 1.9, characters are never mysteriously turned into integers. If you want to get a character's ASCII value, you can use the ord method, like ?a.ord (which returns 97).

Question marks instead of non-ASCII symbols in Ruby gets using Windows terminal

I've found a partial solution, maybe someone will find it helpful.

  1. Change system locale in your Windows to Ukrainian/Belarusian/Russian etc (Control Panel -> Date and Time - > Change date and time -> Change calendar settings -> Administrative -> Change system locale).
  2. Now you've got hexadecimal \xNN\ output for Cyrillic input instead of question marks (like that: \xA2\xE7\xB4). I've found out that it's Windows encoding and by trial and error found the one that suits me - CP866, however, it lacks some characters. So passed string wasn't actually UTF, and we need to force its real encoding explicitly.
  3. I've written this method to get a real UTF-8 string from my gets input, which converts CP866 string to UTF-8 char by char (require 'iconv').
def decode(string)
string.force_encoding('CP866')
Iconv.iconv('utf-8','ibm866', string).join('')
end

Another workaround I'm using is letting user enter UTF input in default notepad app, and then read it from the file, after user pressed some key:

system %{cmd /c "start #{file_to_open}"}
gets
input = File.open('file_to_open').read

ASCII value of character in Ruby

The String#ord method will do the trick:

ruby-1.9.2-p136 > 'x'.ord
=> 120
ruby-1.9.2-p136 > '0'.ord
=> 48
ruby-1.9.2-p136 > ' '.ord
=> 32

Ruby: character to ascii from a string

The c variable already contains the char code!

"string".each_byte do |c|
puts c
end

yields

115
116
114
105
110
103

What does the unary question mark (?) operator do?

It returns a single character string. It is the shortest way to write a single-character string literal. Use it when you want to define a lot of single-character strings. It is a heritage from Ruby <1.9, where it used to return the ASCII code for that character. I don't understand what you mean by "break the language orthogonality".

Invalid multibyte char (US-ASCII) error for ä, ü, ö, ß which are Ascii!

Put the magic comment # coding: utf-8 at the beginning your your script (on the second line if you're using shebang).

#!/usr/local/bin/ruby
# coding: utf-8

puts "i like my chars: ä, ü, ö and ß!"

Ruby 1.9.x undefined method ^ for string

Try text[i].ord ^ round. See Getting an ASCII character code in Ruby using `?` (question mark) fails for more info.



Related Topics



Leave a reply



Submit