'S's won't change if it isn't the first letter in the word


#1

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

If the letter 's' or 'S' are not the first letting in a word on the user_input string, they will not convert to 'th' and the else part of the statement runs. shouldn't the user_input.include? ("S" || "s") statement check the entire string for 's' or 'S' at any point in the whole string? I wasn't sure if maybe this was just a glitch, but wanted to check to make sure my code was correct.

print "Give me a string:"
user_input=gets.chomp

while user_input.empty?
    puts "You gotta enter something buddy:"
    user_input=gets.chomp
end

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

else
    puts "there are no Ss here"
end

puts "You are saying it wrong its: #{user_input}"

#2

This expression:

"S" || "s"

evaluates to:

"S"

Therefore, this expression:

user_input.include? ("S" || "s")

is equivalent to:

user_input.include? ("S")

Here's an example showing you that your code does indeed do its thing even when the 'S' is not the first character:

Give me a string:yummy bananaS
You are saying it wrong its: yummy bananaTh

And besides, if there's no S or s in the text, the substitution has no effect, so why check first? Just do the substitution either way.


#3

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