What is the difference between this two code?


#1



Hi, I am confused between these two codes as shown below. For the first code, there seems to be an issue gsubbing S to th, while subbing s to th works. On the other hand, the second code works perfectly for both S and s. Can somebody help explaining the difference between the first and second code?


print "input please"
user_input = gets.chomp
if user_input.include? "s" || (user_input.include? "S")
    user_input.gsub!(/s/, "th")
    user_input.gsub!(/S/, "th")
else
    print "there is no s \n" #\n enters a new line for next string

end

puts "my string is #{user_input}"

vs
  
print "input please"
user_input = gets.chomp
if (user_input.include? "s") || (user_input.include? "S")
    user_input.gsub!(/s/, "th")
    user_input.gsub!(/S/, "th")
else
    print "there is no s \n" #\n enters a new line for next string

end

puts "my string is #{user_input}"


#2

@digitalwhiz67993 ,

It is an issue of precedence of operations. To get the || to perform the final operation, after both include? methods have had an opportunity to return if necessary, this ...

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

... must be modified to this, with the inclusion of the parentheses ...

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

#3

Hi, let me clarify, so what you are saying is that this is just a fix set of rule/law in Ruby language that we have to follow too because there are no reason or explanation behind it. Please enlighten thank you


#4

It is a rule that operators have precedence over method calls. In the first example, the || operator is applied before the left-side include? method call gets invoked. Therefore, this expression gets passed to user_input.include? ...

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

That is not what we need to happen here. In the second example, we are using parentheses to enclose each of the expressions that we intend as the two operands of the || operator. The two operands then become this ...

(user_input.include? "s")

... and this ...

(user_input.include? "S")

..., which is what we want.

An alternate way to do this would be ...

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

In the above, parentheses are used to delimit the extent of the arguments that are passed to the .include? method.

Regarding another less important issue, note that in the above, this line from the original code ...

user_input.gsub!(/S/, "th")

has been modified to this ...

user_input.gsub!(/S/, "Th")

... in order to preserve case.

Now, "Silly soup" becomes "Thilly thoup".


#5

Hi when i tried using this if

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

or

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

There will be an error as follow
(ruby):2: syntax error, unexpected tSTRING_BEG, expecting keyword_then or ';' or '\n'
if user_input.include? "s" || user_input.include? "S"
^
(ruby):5: syntax error, unexpected keyword_else, expecting $end
What went wrong ? I am slow on this please be patient if im not making sense to you


#6

@digitalwhiz67993 ,

In this first example ...

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

... the following gets passed as the argument to the first occurrence of user_input.include?, because that method call gets invoked last, after everything to the right of it is resolved ...

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

In the second example, ...

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

... the parentheses have grouped the first method call with the if, separating the second call from it. On the two sides of the || operator, you have this ...

(if user_input.include? "s")

... and this, as operands ...

user_input.include? "S"

Neither one of the examples will work correctly.

Ruby offers a lot of syntactic flexibility, which is nice, but it can make it easy to make mistakes. However, you can use parentheses to group together the operations you want to perform first.


#7

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