Module: RLP::Extensions::Array

Defined in:
lib/core_ext/array.rb

Instance Method Summary collapse

Instance Method Details

#as_intObject



12
13
14
# File 'lib/core_ext/array.rb', line 12

def as_int
  self.reverse_bytes
end

#as_stringObject



8
9
10
# File 'lib/core_ext/array.rb', line 8

def as_string
  self.collect{|x| x.as_string}.join
end

#get(position) ⇒ Object



4
5
6
# File 'lib/core_ext/array.rb', line 4

def get(position)
  self[position]
end

#reverse_bytesObject



32
33
34
35
36
37
38
39
40
# File 'lib/core_ext/array.rb', line 32

def reverse_bytes
  bytes = self.collect do |item|
    item.chr
  end
  (8 - bytes.length).times do
    bytes.insert(0, 0.chr)
  end
  bytes.join.unpack("q>").first
end

#to_rlpObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/core_ext/array.rb', line 16

def to_rlp
  return [0xc0] if self == []

  result = self.collect do |item|
    rlp = item.to_rlp
    if rlp.is_a?(Array)
      rlp.to_a.collect{|x| x.chr}
    else
      rlp.chr
    end
  end.flatten

  result = result.join.to_rlp(true)
  return result
end