-1
def roll_die
  rand(1..6)
end

if roll_die == 1
  puts "ruby"
elsif roll_die == 2
  puts "uW"
elsif roll_die == 3
  puts "eBay"
elsif roll_die == 4
  puts "meditate"
elsif roll_die == 5
  puts "musiClean"
else
  puts "medicate"
end

How do I decrease the redundancy in my code?

2
  • 2
    Your logic is flawed, you're rolling the die multiple times.
    – August
    Oct 11, 2014 at 1:33
  • 1
    This question appears to be off-topic because it is about code refactoring and review, and should be on Code Review instead. Oct 11, 2014 at 23:19

2 Answers 2

8

How about using Array#sample:

['ruby', 'uW', 'eBay', 'meditate', 'musiClean', 'medicate'].sample
4

Create an array, and access it by index.

rolls = %w(ruby uW eBay meditat musiClean medicate)

puts rolls[roll_die - 1]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.