ruby - Why can't I modify values of an array in a method? -
in code fragment below, array deck
should equal [6,9,5,6,5,1,2]
since ruby passes arrays reference. after method call, values of deck not change. why that?
def count_cut!(deck) s1, s2 = deck[0, deck.last], deck[deck.last..-2] deck = s2 + s1 + [deck.last] end deck = [5, 1, 6, 9, 5, 6, 2] count_cut!(deck) p deck
i using ruby 1.9.2-p180.
assigning deck
not mutate array passed in. treated local variable in scope of function definition. can call deck.replace
if want mutate it.
Comments
Post a Comment