1

So I am doing a small, simple project for my class and for some reason I can't access a value using a variable.

This is my class: (I am having problems with the getAnswer method, in particular the answerArray array)

#Create random fact array
class RandomFact
    def initialize()
        @randomNum = rand(5)
    end

    def getQuestion
        randomNum = @randomNum
        questionArray = Array.new
        questionArray[0] = "Do you liek mudkipz?"
        questionArray[1] = "Question2"
        questionArray[2] = "Three"
        questionArray[3] = "Reddit"
        questionArray[4] = "4chan"

        puts questionArray[randomNum]
        return randomNum
    end

    def getAnswer(randomNum,answer)
        answerArray = Array.new
        answerArray[0] = "one"
        answerArray[1] = "two"
        answerArray[2] = "three"
        answerArray[3] = "four"
        answerArray[4] = "five"

        return answerArray[randomNum]
    end

end

This is my class to the class:

    randomNum = cgi['randomNum']
    answer = cgi['answer']
    puts newQuestion.getAnswer(randomNum,answer)

Now the thing is that randomNum holds a value from a previous form. If I print out randomNum right under where I pull the value from the form I get it.
if I print out randomNum inside of the method getAnswer I get it.
If I print out answerArray[0] I get a value.
If I print out answerArray[randomNum] I get nothing.



It is pretty much an exact copy of the getQuestion method from above and that one works. Any input?

1
  • do a p randomNum and p answer so we can see details about randomNum and answer. FYI p obj is the equivalent of puts obj.inspect - it gives more information than puts obj.to_s and is generally more useful for debugging. Commented Apr 11, 2009 at 3:16

5 Answers 5

1

The random number is probably coming in as a string from your CGI. Cast it to an integer using randomNum.to_i and you'll be set.

Sign up to request clarification or add additional context in comments.

1 Comment

Nope, I had tried to change it using int() earlier. I just tried to_i and that didn't work either.
0

It must be your input. When I tried it in irb, it worked for me.

>> new_q = RandomFact.new
=> #<RandomFact:0x41028e74 @randomNum=2>
>> new_q.getQuestion
Three
=> 2
>> new_q.getAnswer(2, "")
=> "three"

1 Comment

I know its my input, I just don't know what is wrong with it. I have tried to convert the input to make sure it is an int and it still does not work. The input is there though, I can print it out I just can't use it in conjunction with the array for some reason.
0

I'm not sure what you're really trying to do here, but if you're really only trying to do what your example shows, then you don't need to be creating classes and functions. A single array of questions and answers would work great.

QA = [
  ["What is the capital of Estonia?","Tallinn"],
  ["How many times to 6 go into 18?","3"],
  ["What have I got in my pocket?","The Ring"]
]

qnum = cgi["qnum"].to_i
question = QA[qnum].first
answer = QA[qnum].last

1 Comment

It's for a class I am taking on Ruby, so we kind of have to take the long way around to learn about the language. But yeah, that would be an ideal way to do it.
0

Are you initializing newQuestion somewhere? In the example you gave it will be nil.

A couple of not-relevant-to-the-question tidbits: you don't need the return statement. Ruby always returns the last value. Just put the value there by itself. And in Ruby the standard practice is to write your variables as new_question, not newQuestion.

5 Comments

Yes I am I initiate it right after my Print-content line, it just isn't in the example above. I noticed my teacher writing the variables like that, is there any reason why?
What print-content line? You means after the puts? If so then that of course won't work. Writing variables that way will work, it just goes against the Ruby convention. If you include the missing parts of your code you'll be more likely to get a better answer.
what do you get for answerArray[randomNum.to_i]?
Ok, doing answerArray[randomNum.to_i] worked but how come doing it in two steps didn't? randomNum.to_i answerArray[randomNum.to_i]
because you're not saving the result anywhere. If you do randomNum = randomNum.to_i it will be preserved. Of coruse it would be asier to simply do it right away: randomNum = cgi['randomNum'].to_i
0

There's a number of things in your code that look quite irregular.

Describing the questions and answers in two separate arrays is trouble no matter how you slice it. What's better is to define them in a simple, consistent array of arrays, then pick one of those elements using the built-in Array#rand method that will select one of them at random.

For example


  class RandomFact
    QUESTIONS = [
      [ 'How many nuts can a squirrel eat?', '2' ],
      [ 'What is my favorite color?', 'blue' ]
    ]

    def self.rand
      QUESTIONS.rand
    end
  end

  (question, answer) = RandomFact.rand

  puts "Question: #{question}"
  puts "Answer: #{answer}"

2 Comments

Which version of Ruby are you referring to? Neither Ruby 1.8.6, nor 1.8.7, nor 1.9.1 has Array#rand. Ruby 1.9.1 has Array#sample, which does what you want, but no Array#rand.
This must be one of those core extensions from Rails that I've forgotten was part of that package. Thanks for the clarification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.