Seems like elsif don't work


#1

Here's my problem. My code is

print "string"
user_input = gets.chomp
until user_input.length > 0
    print "string, do it"
    user_input = gets.chomp
end
if user_input.include? "s"
    user_input.gsub!(/s/, "th")
elseif user_input.include? "S"
    user_input.gsub!(/S/, "TH")
else 
    puts "no s here"
end
puts "final - #{user_input}"

And if i type something like "sssSSS" i get output "thththSSS", but, if i separate ifs, like

print "string"
user_input = gets.chomp
until user_input.length > 0
    print "string, do it"
    user_input = gets.chomp
end
if user_input.include? "s"
    user_input.gsub!(/s/, "th")
end
if user_input.include? "S"
    user_input.gsub!(/S/, "TH")
else 
    puts "no s here"
end
puts "final - #{user_input}"

the output i get is "thththTHTHTH"

can someone explain why so?


#2

When you are working your way down an if/else statement, as soon as you get a "true" you don't go any further. That's why the "S" part of the first example doesn't work, it doesn't even get looked at because processing stops in the "s" section.


#3

If we assume case sensitivity, this line should be seen when user_input is S. It may be the elseif is not recognized by Ruby. Try, elsif.