mardi 1 mai 2018

RUBY enter with no input as a valid boolean

I'm writing some very simple code, asking for confirmation on a text input, and what I want to do is that if the users simply presses "Enter", make it count as a "yes". For example:

define method
        puts "enter some text"
        @text= gets.chomp
        puts "you entered '#{@text}', is it correct?"
        correct = gets.chomp    
             if correct == 'y' || ''
             other_method
             else
             method
        end
end

But when I run it on Ruby, I get the "Warning, literal string in condition", and whatever you enter, calls the "other_method". The solution I found is the following:

define method
        puts "enter some text"
        @text= gets.chomp
        puts "you entered '#{@text}', is it correct?"
        correct = gets.chomp    
             if correct == 'y'
             other_method
             elsif correct == ''
             other_method
             else
             method
        end
end

But it's pretty annoying, I'd rather understand why the first one doesn't work, and how can I make it work using the | |

Thank you!

Aucun commentaire:

Enregistrer un commentaire