What's the opposite of chr() in Ruby?
If String#ord didn't exist in 1.9, it does in 2.0:
"A".ord #=> 65
Trying to understand the Ruby .chr and .ord methods
According to Integer#chr
you can use the following to force the encoding to be UTF_8.
22909.chr(Encoding::UTF_8)
#=> "好"
To list all available encoding names
Encoding.name_list
#=> ["ASCII-8BIT", "UTF-8", "US-ASCII", "UTF-16BE", "UTF-16LE", "UTF-32BE", "UTF-32LE", "UTF-16", "UTF-32", ...]
A hacky way to get the maximum number of characters
2000000.times.reduce(0) do |x, i|
begin
i.chr(Encoding::UTF_8)
x += 1
rescue
end
x
end
#=> 1112064
How to use .ord and .chr properly in a loop?
You want a mapping like this:
input: abcdefghijklmnopqrstuvwxyz
output: wxyzabcdefghijklmnopqrstuv
Unfortunately your approach doesn't work for the first 4 letters:
("a".ord - 4).chr #=> "]"
("b".ord - 4).chr #=> "^"
("c".ord - 4).chr #=> "_"
("d".ord - 4).chr #=> "`"
I'd use String#tr
. It replaces each occurrence in the first string with the corresponding character in the second string:
"m^aerx%e&gsoi!".tr("abcdefghijklmnopqrstuvwxyz", "wxyzabcdefghijklmnopqrstuv")
#=> "i^want%a&coke!"
There's also a "c1-c2 notation to denote ranges of characters":
"m^aerx%e&gsoi!".tr("a-z", "w-za-v")
#=> "i^want%a&coke!"
The documentation further says:
If
to_str
is shorter thanfrom_str
, it is padded with its last character in order to maintain the correspondence.
So it can be used to easily replace the "special characters" with a space:
"m^aerx%e&gsoi!".tr("a-z@#$%^&*", "w-za-v ")
#=> "i want a coke!"
gsub non ASCII character in Ruby
>> string="foo\xC2bar"
=> "foo\xC2bar"
>> string.force_encoding"ASCII-8BIT"
=> "foo\xC2bar"
>> string.gsub(194.chr, '')
=> "foobar"
English words (ONE, TWO, THREE) to number(1,2,3) in ruby
I created a hash up-to twenty for all words and only for tens(thirty, forty, fifty etc.). Using Reg-ex took off two words and added them for example twenty two is addition of 20+2=22; right now my script only works till hundred, but it can be extended for numbers over 100 and so on.
How to identify character's language in Ruby/Rails?
The UNICODE standard is divided into "blocks". Go here:
http://www.unicode.org/charts/
http://en.wikipedia.org/wiki/Unicode_block
http://www.unicode.org/versions/Unicode6.0.0/
and find unicode blocks (intervals) for each language.
My guess:
- English
- Hebrew
- Russian
So for you its the matter of simple number comparsion for each character (unicode ordinal value). Very simple.
Integer value of a character in ruby?
You probably want String#ord
:
% irb
ruby-1.9.2-head > 'a'.ord
=> 97
Related Topics
Generate All Possibles Combinations of an Array with a Length Within a Given Range
How to Get Url of Active Storage Image
Two Controllers for One Shared View in Ruby on Rails
Where to Put Common Code Found in Multiple Models
How to Globally Configure Rspec to Keep the '--Color' and '--Format Specdoc' Options Turned On
Rspec Allow/Expect VS Just Expect/And_Return
How to Update Ruby with Homebrew
Openssl VS Gpg for Encrypting Off-Site Backups
Error Installing Debugger-Linecache in Ruby 1.9.3
How to Make Capybara Check for Visibility After Some Js Has Run
Finding What Is Common to Two Arrays
Find or Create Record Through Factory_Girl Association
Rails Bundler Doesn't Install Gems Inside a Group
How to Get the Number of Days in a Given Month in Ruby, Accounting for Year
Rails-Like Database Migrations