class Trytes

Attributes

value[R]

Public Class Methods

new(string) click to toggle source
# File lib/trytes.rb, line 4
def initialize(string)
  @value = string
  return if trytes?
  @value = ''
  string.each_char do |char|
    asciiValue = char.unpack('c*').first
    return if asciiValue > 255
    firstValue = asciiValue % 27
    secondValue = (asciiValue - firstValue) / 27
    tryte = trytes_chars[firstValue] + trytes_chars[secondValue]
    @value += tryte
  end
end

Public Instance Methods

to_string() click to toggle source
# File lib/trytes.rb, line 18
def to_string
  return value unless trytes?
  string = ''
  (0..(value.length - 1)).step(2) do |i|
    tryte = value[i] + value[i + 1]
    break if tryte == '99'
    firstValue = trytes_chars.index(tryte[0])
    secondValue = trytes_chars.index(tryte[1])
    decimalValue = firstValue + secondValue * 27
    string += decimalValue.chr
  end
  string
end

Private Instance Methods

trytes?() click to toggle source
# File lib/trytes.rb, line 37
def trytes?
  return false unless value.kind_of? String
  return false unless /^[9A-Z]*$/.match(value)
  true
end
trytes_chars() click to toggle source
# File lib/trytes.rb, line 33
def trytes_chars
  '9ABCDEFGHIJKLMNOPQRSTUVWXYZ'
end