Class: Trytes

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Trytes

Returns a new instance of Trytes.



4
5
6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



2
3
4
# File 'lib/trytes.rb', line 2

def value
  @value
end

Instance Method Details

#to_stringObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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