Do it in the Ruby way: instead of modifying the original array, return the modified array:
def count_cut(deck)
s1, s2 = deck[0, deck.last], deck[deck.last..-2]
s2 + s1 + [deck.last]
end
Then the caller assigns the return value deck:
deck = [5, 1, 6, 9, 5, 6, 2]
deck = count_cut(deck)
p deck
Deck, :
class Deck
def initialize(cards)
@cards = cards
end
def count_cut!
s1, s2 = @cards[0, @cards.last], @cards[@cards.last..-2]
@cards = s2 + s1 + [@cards.last]
end
end
deck = Deck.new [5, 1, 6, 9, 5, 6, 2]
deck.count_cut!
p deck