In Ruby everything are objects. The Ruby interpreter assumes that all instance variables belong to the current object self. This is also true in a class definition. The role of self belongs to the class itself, so the instance variable @values belongs to the class. Don’t get confused! Instance variables of the class are different from instance
variables of that class’s objects. Also you don't need specify return
keyword explicitly Try this:
class DiceSet
attr_accessor :values
def roll(number_of_rolls)
@values = []
(1..number_of_rolls).each do |roll|
@values << (1..6).to_a.sample
end
@values
end
end
dice = DiceSet.new
dice.roll(5)
puts dice.values.class
assert dice.values.is_a?(Array)