Value of an expression?


#1

Sorry if this is asked before and I could not find its answer. Take a
look at the following:

1 #!/usr/bin/ruby
2 s=“test”
3 puts s
4 s=“surprising” if (1+1 != 2)
5 puts s

which produces the output:
test
test

but I expected it to output:
test
nil

since I thought the expression in line number 5 (“surprising” if (1+1 !=
2)) should evaluate to nil and hence s should be assigned nil.

So, my question is:

  • Why?
  • What should I read to understand this better?

Thank you!
-Kedar


#2

s = “surprising” if (1+1 != 2)

There are two expressions here, and the if is acting as a modifier. The
first expression (s = “surprising”) is only evaluated if the second
expression (1 + 1 != 2) evaluates as true.

It is the equivalent of:

if (1 + 1 != 2)
s = “surprising”
end

So s = “surprising” is never evaluated, and s continues to equal “test”.

Googling “ruby if modifier” should turn up some results.


#3

It is the equivalent of:

if (1 + 1 != 2)
s = “surprising”
end

So s = “surprising” is never evaluated, and s continues to equal “test”.

Ah ok, thanks. However, I was thinking if it should be nil, not
“surprising”, because of the following experience with irb:

“surprising” if (1+1 !=2)
=> nil

which gives an impression that the value of this entire expression is
nil.

-Kedar


#4

Kedar M. wrote:

test

  • Why?
  • What should I read to understand this better?

Thank you!
-Kedar

This doesn’t change because the string s is “test”. Since 1+1 is 2, it
will not change the value of s (thus it’s still “test”). Only if it
were true would it result in “surprising”. It’s doing exactly what it
should do. The result of the conditional would be nil, but this is why
it’s not changing it, and s remains the same value.


#5

Kedar M. wrote:

Ah ok, thanks. However, I was thinking if it should be nil, not
“surprising”, because of the following experience with irb:

“surprising” if (1+1 !=2)
=> nil

which gives an impression that the value of this entire expression is
nil.

-Kedar

That’s because the conditional isn’t true. If the conditional wasn’t
nil, it would have a result (no result is nil):

irb(main):008:0> s = “surprising” if (1+1 != 2)
=> nil
irb(main):009:0> s = “surprising” if (1+1 == 2)
=> “surprising”
irb(main):010:0>

See, for example:

irb(main):010:0> s = ‘value’
=> “value”

See that “puts s” has no result, you see => nil.
irb(main):011:0> puts s
value
=> nil

It does what it is supposed to do:
irb(main):010:0> s = ‘value’
=> “value”
irb(main):011:0> puts s
value
=> nil
irb(main):013:0> s = “surprising” if (1+1 != 2)
=> nil <- conditional false, nil.
irb(main):014:0> puts s
value
=> nil
irb(main):015:0> s = “surprising” if (1+1 == 2)
=> “surprising” <- conditional true, not nil.
irb(main):016:0> puts s
surprising
=> nil

These results don’t change what ‘s’ is assigned, unless it’s true and s
is assigned a different value.

I hope that makes sense.


#6

On Jan 9, 2009, at 5:57 PM, Kedar M. wrote:

“surprising”, because of the following experience with irb:

“surprising” if (1+1 !=2)
=> nil

which gives an impression that the value of this entire expression is
nil.

-Kedar

irb is showing the #inspect of the last expression that it evaluates.
It evaluates (1+1!=2) finds that to be false and ‘if false’ means that
“nothing” is returned by the expression on the left (“surprising”).
That nothing is just nil.

You could convince yourself of this by running:

irb> if 1+1 != 2
irb> “surprising”
irb> end
=> nil

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#7

Adam K. removed_email_address@domain.invalid writes:

if (1 + 1 != 2)
s = “surprising”
end

Why is it not equivalent to:

s = if (1 + 1 != 2)
      s = "suprising"
    end

?

(rethorical question).


#8

Kedar M. removed_email_address@domain.invalid writes:

test
test

but I expected it to output:
test
nil

since I thought the expression in line number 5 (“surprising” if (1+1 !=
2)) should evaluate to nil and hence s should be assigned nil.

It does. It should not, because that’s not what you wrote.

So, my question is:

  • Why?

Because.

  • What should I read to understand this better?

The grammar of Ruby. It’s quite insipid. Why would you lose your time
on this, don’t you have anything more interesting to do?

Just write:

(s = (“surprizing” if ((1 + 1) != 2)))

or

((s = “surprizing”) if ((1 + 1) != 2)))

depending on what you mean.


#9

I hope that makes sense.

Ah, it does. Thanks. I was confusing it what irb “echoes”.


#10

Pascal J. Bourguignon wrote:

Just write:

(s = (“surprizing” if ((1 + 1) != 2)))

irb would have shown the result as nil for that specific test and they’d
have asked the same question, I think. However, I won’t guess what
they intended to do or how. :slight_smile:

or

((s = “surprizing”) if ((1 + 1) != 2)))
^^^

oops… you meant: ((s = “surprizing”) if ((1 + 1) != 2))


#11

Tim G. removed_email_address@domain.invalid writes:

or

((s = “surprizing”) if ((1 + 1) != 2)))
^^^

oops… you meant: ((s = “surprizing”) if ((1 + 1) != 2))

Right, I corrected it in the irb buffer and forgot to backpatch the gnus
buffer.


#12

Sorry, I had a glimmer of hope but this script eclipsed it :frowning:
confused.rb

#!/usr/bin/ruby

s=“test”
puts s
s=“surprising” if (1+1!=2)
puts s
s=(“surprising” if (1+1!=2))
puts s

Outputs:
test
test
nil

so, to add to a newbie’s confusion, a pair of parenthesis does the
expected.

-Kedar


#13

Kedar M. wrote:

s=(“surprising” if (1+1!=2))

That is because when it’s all in paranthsis, it is assigning the value
of the conditional. That can be useful when used properly.


#14

Pascal J. Bourguignon wrote:

Kedar M. removed_email_address@domain.invalid writes:

puts s

Outputs:
test
test
nil

so, to add to a newbie’s confusion, a pair of parenthesis does the
expected.

What did you expect?

case 1:
(s = “surprising”) if ((1 + 1) != 2) won’t assign unless (1+1)!=2

case 2:
s = (“surprising” if ((1 + 1) != 2)) will assign the result of the
parenthese, which will be “surprising” if ((1 + 1) != 2) (and nil
otherwise).
If you wanted not to do any assignment to s, then you should go back to
case 1.

Tim and Pascal,

I certainly appreciate your attempts to make me get it. I think I am
close. The only weird thing is I was not sure whether
“surprising” if (1+1 != 2)
is an “expression” and if yes, what its value was. I thought that this
expression is same as (“surprising” if (1+1 != 2)) and both should
evaluate to the same value.

Thus, whereas now I get that the value of (“surprising” if (1+1 != 2))
is nil, it is not clear to me if

  • “surprising” if (1+1 != 2) is an expression and evaluates to some/same
    value.

Apologies if this is becoming rather lengthy.

Regards,
Kedar


#15

Pascal J. Bourguignon wrote:

Why is it not equivalent to:

s = if (1 + 1 != 2)
      s = "suprising"
    end

?

Because of precedence. If you’d write s = (“surprising” if (1+1!=2)), it
would
be equivalent to the above.

(rethorical question).

Oops.


#16

Kedar M. wrote:

so, to add to a newbie’s confusion, a pair of parenthesis does the
otherwise).
evaluate to the same value.

I think the response mode irb defaults to, is confusing you. When you
use irb, for the sake of clarifying what you were expecting previously,
use the --noinspect option (i.e., irb --noinspect).


#17

Kedar M. removed_email_address@domain.invalid writes:

puts s

Outputs:
test
test
nil

so, to add to a newbie’s confusion, a pair of parenthesis does the
expected.

What did you expect?

case 1:
(s = “surprising”) if ((1 + 1) != 2) won’t assign unless (1+1)!=2

case 2:
s = (“surprising” if ((1 + 1) != 2)) will assign the result of the
parenthese, which will be “surprising” if ((1 + 1) != 2) (and nil
otherwise).
If you wanted not to do any assignment to s, then you should go back to
case 1.


#18

Kedar M. removed_email_address@domain.invalid writes:

value.
Is it not the same?

irb(main):698:0> “surprising” if (1+1 != 2)
nil
irb(main):699:0> (“surprising” if (1+1 != 2))
nil


#19

On 10.01.2009 04:43, Kedar M. wrote:

I certainly appreciate your attempts to make me get it. I think I am
close. The only weird thing is I was not sure whether
“surprising” if (1+1 != 2)
is an “expression” and if yes, what its value was. I thought that this
expression is same as (“surprising” if (1+1 != 2)) and both should
evaluate to the same value.

They are the same and they do evaluate to the same value. However,
the point which probably hasn’t become clear so far is this: the
assignment is part of the expression that’s conditionally evaluated. If
the expression isn’t evaluated, no assignment happens.

x = 0
(x = 1) if false
x = 1 if false # same as above

In this case the assignment with 1 is never executed and hence the value
of the variable does not change. The result of the whole expression
however is either nil (in case of the condition evaluating to false) or
the result of evaluating the expression before the “if”.

Assignments happen to be defined as expressions in Ruby (everything is -
there is no distinction between statement and expression like in some
other languages). So they have a result like evaluating any other
expression like (1 + 3) for example. The way it is defined the result
of an assignment expression is the right hand side. You can view the
change of the variable as a side effect of evaluating the assignment
expression.

That’s also the reason why you can do assignment chains like

a = b = c = 0

Assignments are evaluated right to left (as opposed to 1 + 2 + 3 for
example which is evaluated left to right). The result of “c = 0” is a)
c now points to object 0 and b) 0 is also returned as result of
evaluating this, so in the end you get

(a = (b = (c = 0)))

Thus, whereas now I get that the value of (“surprising” if (1+1 != 2))
is nil, it is not clear to me if

  • “surprising” if (1+1 != 2) is an expression and evaluates to some/same
    value.

Yes, it is an expression and evaluates the way I have tried to outline
above. Everything is an expression in Ruby, even a case statement which
comes in handy from time to time:

foo = case x
when String
“it’s a String”
when Fixnum, Integer, Float
“it’s a number”
else
“I have no idea”
end

puts foo

A final remark, if your aim is to assign nil in case the condition is
false, the ternary operator is probably a better choice:

s = (1+1 != 2) ? “surprising” : nil
s = 1+1 != 2 ? “surprising” : nil
s = 1+1 == 2 ? nil : “surprising”

You can as well use if else which is more verbose

s = if (1+1 != 2) then “surprising” else nil end

The main point is to make the assignment unconditional.

Apologies if this is becoming rather lengthy.

No worries. That’s ok.

Kind regards

robert


#20

In this case the assignment with 1 is never executed and hence the value
of the variable does not change. The result of the whole expression
however is either nil (in case of the condition evaluating to false) or
the result of evaluating the expression before the “if”.

Assignments happen to be defined as expressions in Ruby (everything is -
there is no distinction between statement and expression like in some
other languages). So they have a result like evaluating any other
expression like (1 + 3) for example. The way it is defined the result
of an assignment expression is the right hand side. You can view the
change of the variable as a side effect of evaluating the assignment
expression.

Thanks Robert. I get it now.

Regards,
Kedar