Why do newlines within parenthesis change arithmetic results? - ruby

Why do the following expressions resolve the way they do? Brackets should have higher precedence than newlines, shouldn't they?
3 - ( 1 + 1 )
# => 1
3 - ( 1
+ 1 )
# => 2
Omitting the plus also lets the expression evaluate to 2:
3 - ( 1
1 )
# => 2
If I declare as a continuous newline (escaped) or move the plus to the first line, desired behavior is achieved:
3 - ( 1 \
+ 1 )
# => 1
3 - ( 1 +
1 )
# => 1

It is because Ruby recognizes a new line as an end of an expression unless the expression is incomplete. For example,
(1
+ 1)
is the same as
(1;
+1)
which is the same as +1 since the last expression within the parentheses is returned. And that is further the same as 1.
When you have a + at the end of a line, the expression is incomplete, and hence continues to the next line. That makes:
3 - ( 1 +
1 )
to be interpreted as 3 - (1 + 1).

If you have code in the brackets then every line will be threat as separated code line of code if you won't end it with \ or start new one with math operator.
So in your example:
def plus_minus_if_you_understand_this_the_problem_is_solved_i_guess
3 - (1
1 )
end
Means I have number 3 and want to subtract expression in brackets. In the brackets I have line #1 number 1 and in #2 line number 1 again and as it's last line of expression it's retuned by Ruby (like in def last item before end is returned. So:
( 3 # doing nothing
1 ) # returns 1
Also take a look bellow. Again, this part of code returns 2 as it is the last item in brackets:
( puts "hello!"
2 ) => 2

Related

Ruby: evaluation order depends on whitespace and seemingly redundant parens, why?

I'm trying to understand the behaviour of arithmetic methods in Ruby. 1 .+ 2 .* 3 and 1 .+ (2) .* 3 and 1. + 2. * 3 and 1. + (2). * 3 and 1.+2.*3 all evaluate to 7, which means :+ is called before :*, but 1.+(2).*3 evaluates to 9, which means :* is called before :+. Redefining both methods confirms that that's what's happening:
class Integer
alias_method :add, :+
alias_method :mul, :*
def + other
print :+
self.add other
end
def * other
print :*
self.mul other
end
end
puts 1 .+ 2 .* 3
puts 1 .+ (2) .* 3
puts 1. + 2. * 3
puts 1. + (2). * 3
puts 1.+2.*3
puts 1.+(2).*3
Output:
*+7
*+7
*+7
*+7
*+7
+*9
Why does it happen? Please point me where to find the relevant documentation. Thanks in advance.
Let's give an example that might be easier to understand
puts (2) + 3
vs
puts(2) + 3
In the first case, puts is followed by a space. Ruby takes it as omitted parentheses and evaluates the rest as the argument. It's equvalent to puts((2) + 3). 5 is printed. In the 2nd case, puts is immediately followed by a left parenthesis and ruby takes that as the beginning of method arguments. It's equivalent to (puts(2)) + 3. It will print a 2 and then fail when trying to evaluate nil + 3.
Now lets look at your examples
puts 1 .+ 2 .* 3
puts 1 .+ (2) .* 3
puts 1. + 2. * 3
puts 1. + (2). * 3
puts 1.+2.*3
In the first 6 cases, no method call is immediately followed by (, all the parentheses are redundant. They are evaluated right to left. Note that not * before +, but right to left.
puts 1 .* 2 .+ 3 #=> 5
In the last example puts 1.+(2).*3, the ( immediately follows .+, so 2 is an argument to .+. The result is then multiplied by 3.

Is the assignment operator really "just" an operator?

My question was triggered by this discussion on SO, which did not lead to an answer that would really explain the issue. I am "rewriting" it here in a slightly different way, because I want to make it more clear what the real problem is and therefore hope to get an answer here.
Consider the following two Ruby expressions:
1 * a - 3
1 && a = 3
From the Ruby precedence table, we know that of the operators mentioned here, * has the highest precedence, followed by -, then by && and finally by =.
The expressions don't have parenthesis, but - as we can verify in irb, providing a suitable value for a in the first case - they are evaluated as if the bracketing were written as (1*a) - 3, respectively 1 && (a=3).
The first one is easy to understand, since * binds stronger than -.
The second one can't be explained in this way. && binds stronger than =, so if precedence only would matter, the interpretation should be (1 && a) = 3.
Associativity (= is right-associative and - is left-associative) can't explain the effect either, because associativity is only important if we have several operators of the same kind (such as x-y-z or x=y=z).
There must be some special rule in the assignment operator, which I did not find in the docs I checked in particular the docs for assignment and syntax.
Could someone point out, where this special behaviour of the assignment operator is documented? Or did I miss / misunderstand something here?
From the doc: https://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#assign
Assignment expression are used to assign objects to the variables or such. Assignments sometimes work as declarations for local variables or class constants. The left hand side of the assignment expressions can be either:
variables
variables `=' expression
On the right there is an expression, so the result of the expression is assigned to the variable.
So, you should look for expressions (*) before following the precedence.
1 && a = 3 are basically two "chained" expressions:
3 and 1 && 3
Maybe it is more readable as:
1 && a = 3 + 4 where the expressions are 3 + 4 and 1 && 7, see:
1 && a = 3 + 4 #=> 7
1 && 7 #=> 7
res = 1 && a = 3 + 4
res #=> 7
(*) The precedence table also helps to find the expression (Find the precedence table in the linked doc at the Operator expressions paragraph):
What's above the = in the table "forms" an expression to be assigned by =, what's below does not.
For example:
1 + 3 and 2 + 4 #=> 4
a = 1 + 3 and b = 2 + 4 #=> 4
(a = 1 + 3) and (b = 2 + 4) #=> 4
a = (1 + 3 and b = 2 + 4) #=> 6
You can also check these examples respect to the precedence table:
1 && 3 #=> 3
1 && a = 3 #=> 3
a #=> 3
3 and 1 #=> 3
3 and b = 1 #=> 3
b #=> 1
2 ** c = 2 + 1 #=> 8
c #=> 3
d = 2 ** 3
d #=> 8
e = 3
e **= 2
e #=> 9
I think the understanding of 1 && (a = 3) is, understandably, mislead.
a = false
b = 1
b && a = 3
b
=> 1
a
=> 3
Why is a being assigned to in the && expression when a is false? Should the && expression not return when encountering a false value? Spoiler, it does return!
Taking a step back, we think of the purpose of the && operator to control the flow of logic. Our disposition to the statement
1 && a = 3
is to assume the entire statement is returned if a is nil or false. Well no, the interpreter is evaluating like so:
(1 && a) = 3
The interpreter does not raise a if it is nil or false nor does it return the left side if a is nil or false
a = nil
1 && a
=> nil # a was returned
The interpreter returns the variable, this is why the original statement can be read:
a = 3
due to 1 && a returning a which is a variable that can be assigned to by the = operand on the second half of the statement.
TLDR
In your origin example: 1 is neither nil nor false so the variable a is returned in (1 && a) which is subsequently assigned in a = 3
Probably because the other interpretation does not work:
irb(main):003:0> (1 && a) = 3
Traceback (most recent call last):
3: from /home/w/.rbenv/versions/2.7/bin/irb:23:in `<main>'
2: from /home/w/.rbenv/versions/2.7/bin/irb:23:in `load'
1: from /home/w/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
SyntaxError ((irb):3: syntax error, unexpected '=', expecting `end')
(1 && a) = 3
^
So, perhaps Ruby parenthesizes 1 && a = 3 in the only way that is legally interpretable by the language.

Count characters starting at zero?

I need to write a for-each loop that lists each character in
mystery_string with its index. Example below:
mystery_string= "Olivia," output would be:
0 O
1 l
2 i
3 v
4 i
5 a
I cannot use the range function on this problem.
This is my code, but the number starts at 1. What am I doing wrong?
mystery_string = "CS1301"
count = 0
for current_letter in mystery_string:
count = count + 1
print (count , current_letter)
I have been getting this as output:
1 C
2 S
3 1
4 3
5 0
6 1
but it needs to start at zero.
Just add the count (count += 1) after you print in the for loop
Note: Also, please format your code in a code block surrounded with a tick(`) or multiline code with 3 tick (```)
The pythonic way is to use enumerate() in such a case. This way you'll get both the index and the content of your string.
mystery_string = "CS1301"
for count, current_letter in enumerate(mystery_string):
print (count , current_letter)

Elixir: Return value from for loop

I have a requirement for a for loop in Elixir that returns a calculated value.
Here is my simple example:
a = 0
for i <- 1..10
do
a = a + 1
IO.inspect a
end
IO.inspect a
Here is the output:
warning: variable i is unused
Untitled 15:2
2
2
2
2
2
2
2
2
2
2
1
I know that i is unused and can be used in place of a in this example, but that's not the question. The question is how do you get the for loop to return the variable a = 10?
You cannot do it this way as variables in Elixir are immutable. What your code really does is create a new a inside the for on every iteration, and does not modify the outer a at all, so the outer a remains 1, while the inner one is always 2. For this pattern of initial value + updating the value for each iteration of an enumerable, you can use Enum.reduce/3:
# This code does exactly what your code would have done in a language with mutable variables.
# a is 0 initially
a = Enum.reduce 1..10, 0, fn i, a ->
new_a = a + 1
IO.inspect new_a
# we set a to new_a, which is a + 1 on every iteration
new_a
end
# a here is the final value of a
IO.inspect a
Output:
1
2
3
4
5
6
7
8
9
10
10

What's the fastest way to check if input String is a correct RPN expression?

I came across a task which makes you check if a String passed as an argument to your method/function is a correct statement in the Reverse Polish Notation sense. It can contain lowercase alphabet letters, operation signs and integers. Is there any faster way to check it than to read every character separately and actually try to evaluate the whole expression?
You don't have to evaluate the whole expression but you do need to split it into tokens, and you have to know the valence of every operator (that is, how many operands it takes). For simplicity, let the valence of an operand be 0; then do the following:
Set stack_size to 0;
For Each token In expression:
Set stack_size to stack_size + 1 - valence(token)
If stack_size <= 0: Report failure
If stack_size == 1: Report success
Else : Report failure
Examples using _ for unary minus.
expression: 3 4 + 1 * _
stack_size: 0 1 2 1 2 1 1 -> success
expression: 2 3 4 + 1 * _
stack_size: 0 1 2 3 2 3 2 2 -> failure (not 1 at the end)
expression: 2 3 + + 1 * _
stack_size: 0 1 2 1 0 -> failure (stack_size <= 0)
You can use a parse table to recognize reverse polish notation. It requires looking at each character, but it's fast.

Resources