Class: Xencoder::Xbase
- Inherits:
-
Object
- Object
- Xencoder::Xbase
- Defined in:
- lib/xencoder/xbase.rb
Instance Attribute Summary collapse
-
#chars ⇒ Object
readonly
Returns the value of attribute chars.
Instance Method Summary collapse
-
#initialize(chars) ⇒ Xbase
constructor
A new instance of Xbase.
- #to_i(str) ⇒ Object
- #to_x(num) ⇒ Object
Constructor Details
#initialize(chars) ⇒ Xbase
Returns a new instance of Xbase.
4 5 6 |
# File 'lib/xencoder/xbase.rb', line 4 def initialize(chars) @chars = chars.dup.freeze end |
Instance Attribute Details
#chars ⇒ Object (readonly)
Returns the value of attribute chars.
3 4 5 |
# File 'lib/xencoder/xbase.rb', line 3 def chars @chars end |
Instance Method Details
#to_i(str) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/xencoder/xbase.rb', line 8 def to_i(str) sum = 0 str.chars.reverse.each_with_index do |char, i| sum += chars.index(char) * chars.size**i end sum end |
#to_x(num) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/xencoder/xbase.rb', line 16 def to_x(num) str = chars.at(num % chars.size) num /= chars.size str = "#{to_x(num)}#{str}" if num > 0 str end |