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__