6. Setting Up the 'Else' Branch


#1

https://www.codecademy.com/courses/ruby-beginner-en-JdNDe/0/6?curriculum_id=5059f8619189a5000201fbcb

If I type anything containing an s in the console, the code works and I can move on. However, if I type anything that does not contain an s, I receive the following error messages:

undefined method 'include?' for nil:NilClass
Oops, try again. Your code doesn't look quite right. Check the first exercise if you need help!

print "Thay Thomething:"
user_input = gets.chomp
user_input = user_input.downcase!

if user_input.include? "s"
    user_input.gsub!(/s/, "th")
    puts user_input
else
    puts "There are no \"s\"'s in your string..."
end

#2

Problem comes from here

user_input = user_input.downcase!

It'll return Nil if no changes were made.If you type a string that is lowercase then you'll get that error.remove the ! or do the following.

user_input.downcase! # don't store to user_input

Run this

print "type lowercase string"
user_input = gets.chomp
c=user_input.downcase!

puts user_input.class
puts c.class

#3

Thanks, that totally worked.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.