class Nstrct::Instruction
Attributes
arguments[RW]
code[RW]
Public Class Methods
build(code, *args)
click to toggle source
Build an instruction for a code and some arguments.
Message.build_instruction 54, [:int8, [7, 8, 9]]
# File lib/nstrct/instruction.rb, line 23 def self.build(code, *args) arguments = args.map do |arg| if arg[1].is_a?(Array) Nstrct::Argument.new(arg[0], arg[1], true) else Nstrct::Argument.new(arg[0], arg[1], false) end end self.new(code, arguments) end
new(code, arguments = [])
click to toggle source
Instantiate a new instruction by its code and alist of arguments
# File lib/nstrct/instruction.rb, line 35 def initialize(code, arguments = []) @code, @arguments = code, arguments end
parse(data)
click to toggle source
Parse one message from the data stream.
# File lib/nstrct/instruction.rb, line 8 def self.parse(data) code = data.slice!(0..1).unpack('s>')[0] num_arguments = data.slice!(0).unpack('C')[0] data.slice!(0..1) # num_array_elements arguments = [] num_arguments.times do arguments << Nstrct::Argument.parse(data) end self.new(code, arguments) end
Public Instance Methods
argument_values()
click to toggle source
Get all the arguments values
# File lib/nstrct/instruction.rb, line 40 def argument_values @arguments.map { |arg| arg.value } end
array_elements()
click to toggle source
get all elements in arrays
# File lib/nstrct/instruction.rb, line 45 def array_elements @arguments.inject(0) { |sum, a| sum + (a.array ? a.value.is_a?(Array) ? a.value.size : 0 : 0) } end
inspect()
click to toggle source
Return a comparable inspection
# File lib/nstrct/instruction.rb, line 58 def inspect "#<Nstrct::Instructon code=#{@code.inspect}, arguments=#{@arguments.inspect}>" end
pack()
click to toggle source
Pack a single instruction in a buffer
# File lib/nstrct/instruction.rb, line 50 def pack message = [@code].pack('s>') message += [@arguments.size].pack('C') message += [array_elements].pack('s>') @arguments.inject(message) { |msg, arg| msg + arg.pack } end