SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with error - undefined class method

    WARNING: Ruby n00b question!

    I'm trying to solve the below problem for a class I'm taking, but keep getting the following errors. Can someone tell me what I'm doing wrong?

    Implement a new Die class which takes an array of strings as its input. When Die#roll is called, it randomly returns one of these strings. If Die.new is passed an empty array, raise an ArgumentError. It should work like this:

    Code:
    die = Die.new(['A', 'B', 'C', 'D', 'E', 'F'])
    die.sides # still returns the number of sides, in this case 6
    die.roll # returns one of ['A', 'B', 'C', 'D', 'E', 'F'], randomly
    My code:

    Code:
    class Die
      def initialize(labels)
        if labels.empty?
          raise ArgumentError.new("Your die must have 1 or more sides!")
        else
          @sides = labels.length
       end
    end
    
    def sides
      @sides
    end
    
    def roll
      (1..@sides).each do |x|
      return #What goes here? Need strings in the array to correspond to variable @sides...
      end
    end
    
    end
    Errors I'm getting:

    Error!
    Die#roll returns a single letter if one label is passed in
    expected: ["J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J", "J"] got: [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil] (compared using ==) Diff: @@ -1,101 +1,101 @@ -["J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J", - "J"] +[nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil, + nil]

    Error!
    Die#roll returns every possible letter for a sufficiently large number of rolls
    expected: ["C", "E", "G", "H", "K", "M", "N", "O", "P", "Q", "R", "T", "V", "W", "Z"] got: [nil] (compared using ==) Diff: @@ -1,2 +1,2 @@ -["C", "E", "G", "H", "K", "M", "N", "O", "P", "Q", "R", "T", "V", "W", "Z"] +[nil]


    Anybody know what I'm doing wrong? Would really appreciate some help from an expert.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,895
    Mentioned
    212 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    You can do it like this:

    Code Ruby:
    class Die
      def initialize(labels)
        if labels.empty?
          raise ArgumentError.new("Your die must have 1 or more sides!")
        else
          @labels = labels
        end
      end
     
      def sides
        @labels.length
      end
     
      def roll
        @labels.sample
      end
    end
     
    die = Die.new(['A', 'B', 'C', 'D', 'E', 'F'])
    p die.sides 
    p die.roll

    To return a random element from an array, you can use Array#sample

    HTH

  3. #3
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THANK YOU, PULLO!

  4. #4
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Pullo,

    I have another question about a different problem. Do you mind if I direct message you?

    - Eufonik

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,895
    Mentioned
    212 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Quote Originally Posted by Eufonik View Post
    I have another question about a different problem. Do you mind if I direct message you?
    I would prefer it if you started a new thread in the (Ruby) forum.
    That way, not only might your question benefit someone else in the future, but it will also be seen by more rubyists who may contribute to the answer.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •