Good day.
I just started to study Ruby and the following question arose.

puts "How much $ would you like to earn?" goal=gets.chomp.to_f 

How easy is it to check that the user entered data in the dd.cc format (d-dollar, c-cents) - 3.40, 100.50, 60.00?
And if the user has entered incorrectly - to display an error message.
Thank you.

    1 answer 1

    One possible option is to use a regular expression. In this case, the information entered by the user should not immediately lead to Float , first you should check whether the entered value corresponds to the specified format, and then lead to Float

     puts 'How much $ would you like to earn?' goal = gets.chomp puts 'Wrong pay format' unless goal =~ /\A\d{2}\.\d{2}\z/ puts goal.to_f 
    • raise 'Wrong pay format' - Nakilon
    • @Nakilon yes, it can be so - cheops