mercredi 5 juin 2019

Problem with Value That change for some reason

Just run this code in console. Problem is @notes_before change value after calling method "a.move" for some reasons. How to fix it and why it happen ?

class Slider
  attr_accessor :position
  def initialize(position)
    @position = position
    @notes_before = Array.new
  end

  def move
    @position.each do |note|
      note[0] +=1
    end
    print @notes_before
  end

  def update_pos
    @position.each do |notes|
      @notes_before << notes
    end
    print @notes_before
  end
end

a=Slider.new([[0,0],[0,1]])
a.update_pos
a.move

I expect output of @notes_before to be [[0, 0], [0, 1]] after calling a.move but the actual output is [[1, 0], [1, 1]]

Aucun commentaire:

Enregistrer un commentaire