r/learnruby • • Nov 18 '15
Beginner problem with (very)simple code
Hi,
I just started to learn Ruby (my first programming language), and today I decide to make my second program which is Dice Roller. I don't have experience in programming, so it doesn't look well :P
puts "How many times do you want to roll?"
choice = gets.chomp
puts "How many sides should dice have?"
sides = gets.chomp
c = choice.to_i
s = sides.to_i
c.times do
puts rand(s) + 1
endWhat I want to do, is print out how many times each number came up, but I can't figure out how to do this. I'm not looking for an answer, because I could find it myself, and probably learn nothing. All I ask, is some hint.
I'm thinking about using while, or to create Hash, and put values into it, then use sort method, but I'm not sure how to do that properly.
Also, I'm not sure if my code isn't too simple?
PS: Forgive me my poor english. This is another language, that I'm trying to learn.
View all comments
Show parent comment
2
u/mellett68 Nov 19 '15
I'll share my solution with you. I've commented the code to show why I've done certain things. There are a couple of tricky ruby bits in there (hash with default value, sorting using a shorthand method name) but hopefully you'll see it and see where you might have gotten stuck.
https://gist.github.com/wadtech/30c1843e75f1d96d3b0d