Class: Xencoder::Xbase

Inherits:
Object
  • Object
show all
Defined in:
lib/xencoder/xbase.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#charsObject (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