It didn't print anything


#1

print "String::: "
user_input = gets.chomp.downcase!
print user_input

if I input words written in lower case(ex. asd), it may print nil.
However, it didn’t print anythong.
Isn’t it? Did I understand wrong?


#2

We can’t see it, but it printed nil (nothing, not even a newline) since that was the return value of the downcase! method. Be sure your input contains at least one upper case letter.

The workaround for this is to use the downcase method, instead. It always returns a string whether modified or not.

print "Your input would be appreciated! "
user_input = ""
until user_input.include? "s"
  user_input = gets.chomp.downcase!
  print user_input.nil? ? 'nil' : nil
end
puts "Your string: #{user_input.gsub!(/s/, "th")}"
Your input would be appreciated! hello
nilundefined method `include?' for nil:NilClass
modified
print "Your input would be appreciated! "
user_input = ""
until user_input.include? "s"
  user_input = gets.chomp.downcase!
  user_input = user_input.nil? ? 'nil' : user_input
end
puts "Your string: #{user_input.gsub!(/s/, "th")}"
Your input would be appreciated! hello
Hello
Sheesh
Your string: thheethh

#3

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