6. Setting up 'else' branch


#1

what should i do in here?
my prev code:

print "Please input the string! "
user_input=gets.chomp
user_input.downcase!

if user_input.gets.chomp
else
print "no string"


#2
if user_input.gets.chomp
else
    print "no string"

What are you trying to do here?


#3

THIS WILL WORK
print "string please:"
user_input = gets.chomp
user_input.downcase!

if user_input.include? "s"
user_input.gsub!(/s/, "th")

else user_input.include? "s"

end


#4

I can't tell why this is failing, it seems to be the same as the one rubysurfer48299 used? What am I omitting? it gets an error that include? is undefined.

print "enter text to sound like Daffy Duck"
user_input = gets.chomp.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
else user_input.include? "s" (Note, I tried with just "else" and nothing else and same result.
print "There is no s in this string"
end


#5

Thanks it let me pass


#6

This worked for me. I just figured it was the best way to show the user their entry and explain that is has no "s".

print "where were you born?"

user_input = gets.chomp.downcase!

if user_input.include? "s"
user_input.gsub!(/s/, "th")
else
puts "#{user_input} does not have an s in it"
end


#7

this exercise does not seem to be fitting, it won't work with what they have shown us so far


#8

I can't get passed this exercise. What am I missing?

print "What is your name? "
user_input = gets.chomp.downcase!

if user_input.include? "s"
    user_input.gsub!(/s/, "th")
    
else
    puts "No esses here."
end

#9

Hey hannako

Going to copy-paste my answer from another thread, since you're having the same issue

You're using the self-modifying version of downcase (downcase!) after gets.chomp.
Now, String.downcase will always return a new string which is the lowercase version of the original (documented here), while String.downcase! will modify the original string and return either a lowercase string if anything was changed, or 'nil' if nothing was changed (documented here).

So, if you input a string that contains for instance "Test" (or any other capitalized letters) it will work, while "test" would not. This is because the first would return a lowercase string (since Test contained a capital T, and therefore was changed) while the second would return 'nil'.

To fix this, you only need to remove the "!" sign and it should work fine, or you could assign user_input the value of gets.chomp, and then run "user_input.downcase!" to self-modify the variable.