value of an expression?

K

Kedar Mhaswade

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
 
A

Adam Kittelson

[Note: parts of this message were removed to make it a legal post.]

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.
 
K

Kedar Mhaswade

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:
=> nil

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

-Kedar
 
T

Tim Greer

Kedar said:
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

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.
 
T

Tim Greer

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

=> 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.
 
R

Rob Biedenharn

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

=> 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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
P

Pascal J. Bourguignon

Kedar Mhaswade said:
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.

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.
 
P

Pascal J. Bourguignon

Adam Kittelson said:
[Note: parts of this message were removed to make it a legal post.]

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

Why is it not equivalent to:

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

?



(rethorical question).
 
T

Tim Greer

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. :)
or

((s = "surprizing") if ((1 + 1) != 2)))
^^^

oops... you meant: ((s = "surprizing") if ((1 + 1) != 2))
 
P

Pascal J. Bourguignon

Tim Greer said:
Pascal J. Bourguignon wrote:



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. :)

^^^

oops... you meant: ((s = "surprizing") if ((1 + 1) != 2))

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

Kedar Mhaswade

Sorry, I had a glimmer of hope but this script eclipsed it :(
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
 
T

Tim Greer

Kedar said:
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.
 
P

Pascal J. Bourguignon

Kedar Mhaswade said:
Sorry, I had a glimmer of hope but this script eclipsed it :(
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.

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.
 
K

Kedar Mhaswade

Pascal said:
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
 
S

Sebastian Hungerecker

Pascal said:
Why is it not equivalent to:

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

?

Because of precedence. If you'd write s =3D ("surprising" if (1+1!=3D2)), i=
t would=20
be equivalent to the above.
(rethorical question).

Oops.

=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
T

Tim Greer

Kedar said:
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

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).
 
P

Pascal J. Bourguignon

Kedar Mhaswade said:
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.

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
 
R

Robert Klemme

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
 
K

Kedar Mhaswade

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top