Can't add more substring on .include method


Hi guys, so I'm learning basic ruby right now and doing quite well.
It's just that on this course, I tried to add more condition where the user needs to input "S" or "C" in the user_input. So I put (or) || on the IF condition, but it turns error:

(ruby):8: syntax error, unexpected tSTRING_BEG, expecting keyword_then or ';' or '\n'
if user_input.include? "s"||user_input.include? "c"
(ruby):14: syntax error, unexpected keyword_else, expecting $end

Anyone knows why I can't insert || there? Or how should I do it?
Ruby beginner needs help :smiley: Thanks

puts "Daffy says: "
user_input = gets.chomp

until user_input != ""
    puts "You haven't tell what Daffy would say: "
    user_input = gets.chomp

if user_input.include? "s"||user_input.include? "c" #<-THIS IS THE WEIRD PART
    user_input.gsub!(/s/, "th")
    user_input.gsub!(/S/, "Th")
    user_input.gsub!(/c/, "th")
    user_input.gsub!(/C/, "Th")
    puts "#{user_input}"
    puts "That is not what daffy would say"


It'll be evaluated in this order.. except this isn't valid syntax:


Nope, it's still same error with Ur syntax :slight_smile:
Wonder why I can't put:
if user_input.include? "s" || user_input.include? "c"
Ruby syntax seems simpler than most language.


That wasn't supposed to "work"
It's the order that your code evaluates in. And that order doesn't make sense. You would need to use parentheses to specify the order that it should evaluate in. In particular, arguments to methods without parentheses around them are weaker than the || operator


Ah, I tried to if (user_input.include? "s")||(user_input.include? "c") before and it doesn't work.
But after hearing your suggestion, tried
if ((user_input.include? "s")||(user_input.include? "c"))
and it works!

But what kinda bugs me is:
if pilihan1 == "a" || pilihan1 == "b"
or something like
until pilihan1 == "a" || pilihan1 == "b"

works just fine (without parentheses). But why the aboves are different?

Anyway, seems you help lots of newbies here. Thanks ionatan! :smiley:


No, that'll do what you want because with that the order of evaluation is going to be the one you intended. Putting parentheses around the whole expression does nothing so you must have changed something else as well.
However, most would write a function call like this: f(arg) rather than (f arg) (when it comes to ruby anyway, there are actually a few languages that will put parentheses around the whole thing (notably, lisp))

Operators in expressions are evaluated in particular order, same as how you evaluate multiplication before addition. Arguments with omitted parentheses have very low precedence


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