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.
- 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).
- 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 actuallyUTF
, and we need to force its real encoding explicitly. - I've written this method to get a real
UTF-8
string from mygets
input, which convertsCP866
string toUTF-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
"Stack Level Too Deep" Running Rake Db:Create:All
Validate Words Against an English Dictionary in Rails
How to Download via Http Only Piece of Big File with Ruby
Activerecord::Adapternotspecified Database Configuration Does Not Specify Adapter
How to Check a Word Is Already All Uppercase
Ruby: Accessing Rake Task from a Gem Without Rails
Always Getting 401 Unauthorized with New Install of Rails + Devise
What Is an Illegal Octal Digit
Generate a Powerset of a Set Without Keeping a Stack in Erlang or Ruby
Differencebetween <%= ... %> and <% ... %> in Ruby on Rails
Convert Time to Other Timezone
Convert a .Doc or .Pdf to an Image and Display a Thumbnail in Ruby
How to Find the Key of the Largest Value Hash
How to Extract a Sub-Hash from a Hash
Foreman Only Shows Line with "Started with Pid #" and Nothing Else