0

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.

3
  • Please share the caller code. This class looks fine. Commented Mar 26, 2017 at 7:21
  • @mudasobwa is right in the self.a_16_bit method; I added a comment so you can find it easier Commented Mar 26, 2017 at 7:25
  • 1
    @SebastianDelgado after calling ::a_16_bit a new Rules object 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 by 1. Maybe @variable_position should be a class instance variable? Commented Mar 26, 2017 at 7:52

2 Answers 2

3

When you call a_rules = Rules.new you are creating a new instance of Rules. So the constructor initialize is setting the @variable_position value to 15 once for every instance.

Then, when you call the method bin_val = a_rules.set_variable_value it increases the value of @variable_position once because you are still working on the same instance.

I suggest that @variable_position should be a class variable. That way you won't be resetting it's value every time you instance a new object.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help
1

You don’t need an instance of the object at all:

require "./my_math.rb"
class Rules
  JUMP_ARR = [";JGT", ";JEQ", ";JGE", ";JLT", ";JNE", ";JLE", ";JMP"]

  @variable_position = 15

  # A instructions have to be 16 bit long
  def self.a_16_bit (line)
    bin_val = if line[1] == 'R'
                # code from set_reserved_variable_value(line) here
              elsif (/^[[:alpha:]]+$/).match(line[1..line.length])
                MyMath.to_binary(@variable_position += 1)
              else
                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
end

In your current code you are messing with instance variables and class methods.

1 Comment

Thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.