Class: Valerii
- Inherits:
-
Object
- Object
- Valerii
- Defined in:
- lib/valerii.rb
Constant Summary collapse
- ENCODE_CHARS =
%w( B C D F G H J K M N P Q R S T V W Z b c d f h j k m n p r x t v )
- DECODE_MAP =
ENCODE_CHARS.to_enum(:each_with_index).inject({}) do |h,(c,i)| h[c] = i; h end
Class Method Summary collapse
-
.decode(string) ⇒ Object
Decode a string to an integer.
-
.encode(number, opts = {}) ⇒ Object
Encodes an integer into a string.
Class Method Details
.decode(string) ⇒ Object
Decode a string to an integer
Valerii.decode("KJ") # => 1234
31 32 33 34 35 |
# File 'lib/valerii.rb', line 31 def self.decode(string) string.split(//).map { |char| DECODE_MAP[char] or return nil }.inject(0) { |result,val| (result << 5) + val } end |
.encode(number, opts = {}) ⇒ Object
Encodes an integer into a string
Valerii.encode(1234) # => "KJ"
19 20 21 22 23 24 25 |
# File 'lib/valerii.rb', line 19 def self.encode(number, opts = {}) str = number.to_s(2).reverse.scan(/.{1,5}/).map do |bits| ENCODE_CHARS[bits.reverse.to_i(2)] end.reverse.join return str end |