jeudi 21 juin 2018

Ruby if-statement with strings always go to first case (not rails)

I'm quite new to Ruby. So I have a method that takes string input and then compares it several times. The problem is - no matter what - it always go to the first case. For example:

def method (s)
    if s <=> "help"
        help()
    elsif s <=> "add"
        puts"Enter 2 numbers"
        a = STDIN.gets
        b = STDIN.gets
        add(a,b)
    else
        err("Invalid command, type 'help' for list of available commands")
    end
end

class.method(STDIN.gets)

always do "help()", no matter what the input is. How it should be written?

Aucun commentaire:

Enregistrer un commentaire