Basically, i need to increase by one my variable variable_position every time the method set_variable_value is called, so every bin_val variable created through that method gets created with a different incremental variable_position parameter starting from 16.
Right know, it goes from 15 (see initialize method) to 16 in the first method call, but then i stays at 16 no matter how many times the method is called.
require "./my_math.rb"
class Rules
attr_accessor :variable_position
JUMP_ARR = [";JGT", ";JEQ", ";JGE", ";JLT", ";JNE", ";JLE", ";JMP"]
def initialize
@variable_position = 15
end
# A instructions have to be 16 bit long
def self.a_16_bit (line)
a_rules = Rules.new
if line[1] == 'R'
bin_val = a_rules.set_reserved_variable_value(line)
elsif (/^[[:alpha:]]+$/).match(line[1..line.length])
bin_val = a_rules.set_variable_value #HERE IS WHERE I CALL THE METHOD
else
bin_val = MyMath.to_binary(line[1..line.length])
end
n = bin_val.to_s.length
m = 16 - n
complete_number = ("0"*m) + bin_val.to_s
end
def set_variable_value
@variable_position += 1 #HERE IS WHERE I TRY TO INCREASE THE VALUE
bin_val = MyMath.to_binary(@variable_position)
end
end
Thanks a lot for reading.
::a_16_bita newRulesobject is created. So every time you call that method, you are initializing a new object, with a new instance variable. Plus you call it only once from within::a_16_bit, so it can change only by1. Maybe@variable_positionshould be a class instance variable?