[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

value of an expression?

Kedar Mhaswade

1/9/2009 10:30:00 PM

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
--
Posted via http://www.ruby-....

19 Answers

Adam Kittelson

1/9/2009 10:43:00 PM

0

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

Kedar Mhaswade

1/9/2009 10:58:00 PM

0


> 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

--
Posted via http://www.ruby-....

Tim Greer

1/9/2009 11:20:00 PM

0

Kedar Mhaswade wrote:

> 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.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Tim Greer

1/9/2009 11:27:00 PM

0

Kedar Mhaswade wrote:

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

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.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Rob Biedenharn

1/9/2009 11:31:00 PM

0

On Jan 9, 2009, at 5:57 PM, Kedar Mhaswade wrote:
>> 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

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://agileconsult...
Rob@AgileConsultingLLC.com


pjb

1/9/2009 11:46:00 PM

0

Kedar Mhaswade <kedar.mhaswade@gmail.com> writes:

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

--
__Pascal Bourguignon__

pjb

1/9/2009 11:47:00 PM

0

Adam Kittelson <adam.kittelson@apathydrive.com> writes:

> [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).
--
__Pascal Bourguignon__

Tim Greer

1/9/2009 11:51:00 PM

0

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))
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Kedar Mhaswade

1/9/2009 11:52:00 PM

0


> I hope that makes sense.

Ah, it does. Thanks. I was confusing it what irb "echoes".
--
Posted via http://www.ruby-....

pjb

1/9/2009 11:59:00 PM

0

Tim Greer <tim@burlyhost.com> writes:

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

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

--
__Pascal Bourguignon__