0

First time programmer and I am confused as to why my ruby code is not giving the result I want. I wrote a simple program to simulate rolling different multi-sided dice.

d4 = rand (1..4)
d6 = rand (1..6)
d8 = rand (1..8)
d12 = rand (1..12)
d20 = rand (1..20)
percent = rand (1..100)

puts "Which dice would you like to roll?"
which_dice = gets.chomp

puts "You rolled a #{which_dice}!"

The first six lines define each dice to output a random number between 1 and the number of sides of the dice has. I then ask the user to input the dice they want rolled with the gets method and then out put the result with the last line.

The problem is that when the last line is executed it puts out "You rolled a (what ever the user inputs with the gets method as a string)!". For example, when the user inputs d8 when prompted it would puts "You rolled a d8!" instead of the actually random between 1 and 8 that I want. How can I have it so it puts an actual random number?

KennyP
  • 11
  • 1

4 Answers4

1

You are simply storing a string and then printing it. Why do you expect that string turns into one of those d-something? Anyway, you could do something like

puts "Which dice would you like to roll?"
which_dice = gets.chomp.to_i
if [4, 6, 8, 12, 20, 100].include? which_dice
  number = rand(1..which_dice)
  puts "You rolled a #{number}!"
else
  puts "This dice doesn't exist dude"
end
Ursus
  • 29,643
  • 3
  • 33
  • 50
1

You are capturing a string value in which_dice, not a variable name. An easy way to get around that is to put the random values in an hash, and then use the string value to reference them by name.

dice = {
  "d4" =>  rand(1..4),
  "d6" =>  rand(1..6),
  "d8" =>  rand(1..8),
  "d10" =>  rand(1..10),
  "d12" =>  rand(1..12),
  "d20" =>  rand(1..20)
}

puts "which dice would you like to roll?"
which_dice = gets.chomp

puts "You rolled a #{dice[which_dice]}"

You can also use this for better error handling.

which_dice = dice[gets.chomp] || "non-existent die."
puts "You rolled a #{which_dice}"

will show an error message if they try to roll a d7.

  • Not sure I follow. I wasn't trying to redesign his code, just answer the question that he asked. I'm new to StackOverflow, so I may be approaching this wrong? – Will Keller Mar 01 '17 at 02:00
  • But his question wasn't about to better do the task, it was about why his gets return wasn't accessing his variables. I changed his code the minimum amount to answer the question. But as I said, I'm new here, so I'll keep my eyes open and learn the idioms. – Will Keller Mar 01 '17 at 22:08
0

Short answer to get your output using the same structure of your current script.

puts "You rolled a #{instance_eval(which_dice)}"

However, there a better ways to accomplish this by using different data structures for example.

Codextremist
  • 131
  • 4
  • Be careful with an approach like this, though - https://stackoverflow.com/questions/5349624/how-to-call-methods-dynamically-based-on-their-name/26284769#26284769 – Brad Werth Mar 01 '17 at 01:07
  • Sure @BradWerth ! But as a said, "there are better ways to achieve it". I avoided making assumptions, and wanted only to solve it. Actually, I would say that this is the worst solution from the security perspective, however I could argue, that this solution is the only presented solution that produces the desired result without altering the remaining code. Also, the author didn't tell us the context where this code is running. Maybe he is just trying to solve a school problem, solving a online exercise or running in a environment where the input is controlled (maybe not typed by a human being) – Codextremist Mar 01 '17 at 01:16
  • I just wanted the OP to be aware of the ramifications of evaling user-provided input... – Brad Werth Mar 01 '17 at 01:17
0

So, the quick and dirty response is that you are currently only returning the user inputted text from 'gets.chomp'. You are doing nothing with the variables above. Try this:

d4 = rand (1..4)
d6 = rand (1..6)
d8 = rand (1..8)
d12 = rand (1..12)
d20 = rand (1..20)
percent = rand (1..100)

puts "Which dice would you like to roll?"
which_dice = gets.chomp

case
  when which_dice == 'd4'
    amount = d4
  when which_dice == 'd6'
    amount = d6
  when which_dice == 'd8'
    amount = d8
  when which_dice == 'd12'
    amount = d12
  when which_dice == 'd20'
    amount = d20
  when which_dice == 'percent'
    amount = percent
end

puts "You rolled a #{amount}!"
bchaiken
  • 1
  • 1
  • 3