+ 2

Any ideas for compacting this code down?

I've done this as an exercise in loops. Can anyone suggest ways of compacting it down? test2.rb: def main pick go again end test.rb require 'colorize' def again if $i == $p puts msg(2) # Would you like to go again? answer = gets.downcase.chomp.to_s if answer == "yes" main # Go back to the beginning else msg(4) # Are you sure? confirm = gets.downcase.chomp.to_s case confirm when "yes" msg(3) # Ok then, thanks for playing when "no" main # Go again else msg(5) # Msg for unrecognisable input end end else puts msg(3) # Ok then, thanks for playing end end def pick msg(6) $p = gets.chomp.to_i end def go $i = 0 while $i < $p msg(1) input options sum $i += 1 #count end end def sum a = $p - $i - 1 if a >= 0 puts "You have #{a} goes left..." else again end end def input @b = gets.chomp.to_i end def options if @b <= $p @b elsif @b > $p puts "#{@b}".red + " is too large!".white puts "Try again!" go else puts "Wrong! Try again" go end end def msg(n) case n when 1 puts "Choose a number between 1 and #{$p}".red when 2 puts "Would you like to go again?" when 3 puts "OK then, thanks for playing!" when 4 puts "Are you sure? Yes/No?" when 5 puts "I really don't know what you want from me. I'll assume you don't want to play. Goodbye!" when 6 puts "Pick a number... ".blue end end def count puts "i = #{$i}" end

27th Jan 2018, 10:51 PM
Gareth T. Whitley
Gareth T. Whitley - avatar
3 Answers
+ 5
I'm not very good in Ruby, but I can suggest one thing; please use the Code Playground to type and run your code šŸ˜‰ Just a reminder, it makes it easier for others to help you šŸ‘
28th Jan 2018, 12:44 AM
Learnsolo
+ 4
In case you don't know how to write and run your codes in the code playground, to create a new code press the little plus icon in the code section (On your profile or the trending section) and choose your language ā˜ŗ
28th Jan 2018, 12:46 AM
Learnsolo
0
I know about that, Iā€™m interested in what OTHER people ARE able to do with the code.
28th Jan 2018, 6:04 AM
Gareth T. Whitley
Gareth T. Whitley - avatar