title: Ruby Koans - Array population method affects how it returns
body: <p>I've been working on Ruby Koans and I've gotten to this part that I don't understand too well. Specifically, it's the about_dice_project, where they ask us to define a class to pass several pre-set tests. Originally, my class looked like</p>
  
  <pre><code>class DiceSet
      attr_accessor :values
      def initialize
          @values = []
      end
      def roll(num)
          num.times { |i| @values[i] = rand(1..6) }
          return @values
      end
  end
  </code></pre>
  
  <p>But this failed the test below</p>
  
  <pre><code>dice = DiceSet.new
  
  dice.roll(5)
  first_time = dice.values
  
  dice.roll(5)
  second_time = dice.values
  
  assert_not_equal first_time, second_time,
    "Two rolls should not be equal"
  </code></pre>
  
  <p>And I realized after some testing that both first_time and second_time have the same object_id. So I looked on stackoverflow for some help and found <a href="https://stackoverflow.com/questions/41976353/ruby-koans-test-two-different-sets-of-dices-who-have-same-values">this answer</a>, which doesn't answer my question directly, but I did find a major difference in our code; namely, that instead of using the "num.times" way of rolling the dice, goes instead with</p>
  
  <pre><code>@values = Array.new(num) { rand(1..6) }
  </code></pre>
  
  <p>and this does pass the above test.</p>
  
  <p>After finding this, I was trying to figure out why my original code didn't work, and so I thought that maybe Ruby was passing by reference, and that's why when the dice-rolling-method created a new array instead of modifying the existing array, that was a new object id created. But then I found <a href="https://stackoverflow.com/questions/1872110/is-ruby-pass-by-reference-or-by-value">this question</a>, which says that Ruby is pass-by-value, and if that was the case, then I would have expected that my original way would work.</p>
  
  <p>So now I'm confused again, and I'd like to ask - where did my line of thought go wrong? Is Ruby pass-by-value only in certain cases? Have I misunderstood what pass-by-value/reference even means? Is my issue not related to pass-by-value/reference at all?</p>