Class: String

Inherits:
Object show all
Defined in:
lib/tapyrus.rb

Instance Method Summary collapse

Instance Method Details

#bthObject

binary convert to hex string



100
101
102
# File 'lib/tapyrus.rb', line 100

def bth
  unpack('H*').first
end

#btiObject

binary convert to integer



110
111
112
# File 'lib/tapyrus.rb', line 110

def bti
  bth.to_i(16)
end

#htbObject

hex string convert to binary



105
106
107
# File 'lib/tapyrus.rb', line 105

def htb
  [self].pack('H*')
end

#opcodeObject

get opcode



120
121
122
123
124
125
126
127
128
129
# File 'lib/tapyrus.rb', line 120

def opcode
  case encoding
  when Encoding::ASCII_8BIT
    each_byte.next
  when Encoding::US_ASCII
    ord
  else
    to_i
  end
end

#opcode?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/tapyrus.rb', line 131

def opcode?
  !pushdata?
end

#push_opcode?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/tapyrus.rb', line 135

def push_opcode?
  [Tapyrus::Opcodes::OP_PUSHDATA1, Tapyrus::Opcodes::OP_PUSHDATA2, Tapyrus::Opcodes::OP_PUSHDATA4].include?(opcode)
end

#pushdata?Boolean

whether data push only?

Returns:

  • (Boolean)


140
141
142
# File 'lib/tapyrus.rb', line 140

def pushdata?
  opcode <= Tapyrus::Opcodes::OP_PUSHDATA4 && opcode > Tapyrus::Opcodes::OP_0
end

#pushed_dataObject



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/tapyrus.rb', line 144

def pushed_data
  return nil unless pushdata?
  offset = 1
  case opcode
  when Tapyrus::Opcodes::OP_PUSHDATA1
    offset += 1
  when Tapyrus::Opcodes::OP_PUSHDATA2
    offset += 2
  when Tapyrus::Opcodes::OP_PUSHDATA4
    offset += 4
  end
  self[offset..-1]
end

#rhexObject

reverse hex string endian



115
116
117
# File 'lib/tapyrus.rb', line 115

def rhex
  htb.reverse.bth
end

#valid_hex?Boolean

whether value is hex or not hex

Returns:

  • (Boolean)

    return true if data is hex



160
161
162
# File 'lib/tapyrus.rb', line 160

def valid_hex?
  !self[/\H/]
end