Ruby Tith Meanth war FINAL, where is my fault?


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
As soon as i add the “&& old_input” in Line 2, it wont work anymore. The point of it was to store the original
input of the user seperatly while changing it in the variable “user_input”. Thank you very much for your time!


print “What can we do for you?”
user_input && old_input = gets.chomp
if user_input.length == 0
puts “Well you will have to write something…!”
elsif user_input.include? “s” || “c”
puts “We got ourselves some 's’s and some 'c’s”
user_input.gsub!(/s/, “th”)
user_input.gsub!(/c/, “s”)
puts “The Daffy version, #{user_input}!”
puts “That was your original message ‘#{old_input}’”
print “Nope, no ‘s’ or ‘c’ found”

<do not remove the three backticks above>

You might want to compare “c” to something instead of treating it like a boolean.

Do you have reason to believe that this will have the desired effect? If not, keep in mind there’s no human reading your code and guessing what you want.

So could I change this
“user_input && old_input = gets.chomp”
to this
“user_input = gets.chomp”
“old_input = user_input”

But the thing with the “c” i dont get (i started ruby just yesterday). Thanks for your help.

A human might read your code and guess that you want to test if user_input includes either s or c
But you only test if s is included, and then you say OR "c" as if that was a condition. It’s a character that is just sitting there. You can similarly do this:

if "c"
    puts "c is truthy"
    puts "c is falsy"

There’s no comparison being made, “c” is treated as if it was a true/false value

Again, there’s nothing guessing what you want, so you can’t… make things up and expect it to do what you want. You’ll need to fit pieces together in such a way that it adds up to what you want. You have to look up how to do things.

And when things go wrong, make observations. Find out where it acts differently from what you expected and then look up how to do that something.

i know its a really simple thing but i just dont know how to ask if either this or that is included.
I tried a few things. How is it done correctly?

You can refer to how you would manually do the same thing. What steps are required to check if one of two are included? Well, you’d check if one is included, or the other is included.

This is also where your favorite search engine comes into play. Once you’ve decided what task you want to make happen, you can type that as a search query and somebody else is very likely to have asked about how to do that same thing and others will have made suggestions.

Some things are difficult to search for, but “how do i do x” is not one of them.

The point here is that you will constantly need this kind of information, and you’ll forget it all the time too, meaning you’ll need to look things up again and again. The real skill here is in identifying what you want to know, looking it up and applying it. You can’t learn it all and know what to write all the time. The things you use frequently will stick eventually, so just keep looking up what you need and you’ll 1) get better at it 2) need it a little less as you start picking things up

Yeah, true. Im on it but havent found it yet.

If we then consider what has to be done:
Each character in the string has to be compared to your two characters.
That’s two comparisons for each character in the string, it would be the same number of operations to check if one is in the string and then check if the other one is in the string: if (c is in the string) OR (s is in the string)
Alternatively, loop through and compare to both characters and stop as soon as either is a match

First of all thank you a lot, I didn’t understand the “c” “s” request.

In order for the above line to work, I had to replace it with:
elsif (user_input.include? “s”) || (user_input.include? “c”)

And instead of this:

you would like to have:
user_input = gets.chomp
old_input = user_input.dup

so old_input contains duplicate of, rather than pointer to user_input. Because user_input will change during runtime. And your pointer would point to the changed, Daffified value in puts "That was your original message '#{old_input}'"
rather than to its original (“old”) value.

Reference to .dup, duplicate rather than pointer:
This is my take, highly inelegant, with way too much copying. But utilizing the above two points and working:

print "Your input, please:"
user_input = gets.chomp
old_input = user_input.dup    # .dup

if user_input == ""
    puts "Please type something:"
    user_input = gets.chomp
    old_input = user_input.dup    # .dup

puts ""

# this failed me: elsif user_input.include? "s" || "c"; the below worked:
if (user_input.include? "s") || (user_input.include? "S") || 
(user_input.include? "ce") || (user_input.include? "Ce") || 
(user_input.include? "cE") || (user_input.include? "CE") || 
(user_input.include? "ci") || (user_input.include? "Ci") || 
(user_input.include? "cI") || (user_input.include? "CI") || 
(user_input.include? "cy") || (user_input.include? "Cy") || 
(user_input.include? "cY") || (user_input.include? "CY")
    user_input.gsub!(/s/, "th")
    user_input.gsub!(/S/, "TH")
    user_input.gsub!(/ce/, "s")
    user_input.gsub!(/Ce/, "s")
    user_input.gsub!(/cE/, "s")
    user_input.gsub!(/CE/, "S")
    user_input.gsub!(/ci/, "si")
    user_input.gsub!(/Ci/, "si")
    user_input.gsub!(/cI/, "sI")
    user_input.gsub!(/CI/, "SI")
    user_input.gsub!(/cy/, "sy")
    user_input.gsub!(/Cy/, "sy")
    user_input.gsub!(/cY/, "sY")
    user_input.gsub!(/CY/, "SY")
    puts "#{user_input}"
    puts "We did not find any instances of the letter \"s\"."

puts ""
puts "This was your original input: '#{old_input}'"
1 Like