Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:16:04+00:00 2026-05-27T09:16:04+00:00

Well i’m a ruby newbie and im trying to learn with RubyKoans but i

  • 0

Well i’m a ruby newbie and im trying to learn with RubyKoans but i got stucked with this test

def test_dice_values_should_change_between_rolls
 48     dice = DiceSet.new
 49     dice.roll(5)
 50     first_time = dice.values
 51    
 52     dice.roll(5)
 53     second_time = dice.values
 54     
 55     assert_not_equal first_time, second_time,
 56       "Two rolls should not be equal"
 57   end

and this is DiceSet class

5  class DiceSet
  6    attr_accessor :values
  7 ··
  8    def initialize
  9      @values = []
 10    end
 11 
 12    def roll(times)
 13      @values.clear
 14      times.times do |x|
 15        @values << ( 1 + rand(6))
 16      end
 17     end
 18 ····
 19    end

the thing here is that whenever i run the code it always generates the exact same set of numbers, this is the Output.

Two rolls should not be equal.  <[3, 2, 4, 1, 3]> expected to be != to  <[3, 2, 4, 1, 3]>.

in the test im calling DiceSet.roll two times and for those two times i get the exact same set of ‘random’ numbers when they’re supossed to be diferent right? I figured that i just might create another instance of DiceSet in order to pass the test but im guessing that is not the objective of the test

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:16:05+00:00Added an answer on May 27, 2026 at 9:16 am

    The problem is that DiceSet#values returns a reference to an array, and that array stays the same for the whole lifetime of your DiceSet object. In DiceSet#roll you clear that array and then add new numbers. Since both calls to DiceSet#values return the same reference, the result of the first roll will be lost, and your test is comparing the array with itself.

    I am not familiar with the RubyKoans and what requirements they have, i.e. if you DiceSet is supposed to store the values etc. If it is, then the most straightforward solution is to either use two DiceSets or use Object#dup to store a copy of the returned object for the test.

    Be aware, however, that your test is fragile even with correctly functioning code, as there always is the chance that two consecutive rolls will return the exact same numbers. In this particular case it is relatively small but still very much existent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well friends, I have got this query which works but is very long for
Well this kind of n00b question but I still can't figure it out. I
Well, this one is my XML file <?xml version=1.0 encoding=utf-8?> <config> <setup> <Test>10</Test> <Copy>
Well, I'm trying to make a graphical game for the first time, but: BufferedImage
Well, i've got a nice WPF book its called Sams Windows Presentation Foundation Unleashed.
Well, this is my first post here and really enjoying the site. I have
Well... simple question, right? But with no so simple answers. In firefox i use
Well, it seems simple enough, but I can't find a way to add a
Well this is incredibly frustrating. After being nagged by Rails that I need to
well i have this messages table with sample values like these: msg_id recipient_id read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.