Result of && and 'and'

Hi folks,

is it guranteed that && and “and” return the last value if all values
before that return true? Consider:

irb(main):015:0> x=0
0
irb(main):016:0> x>0 && x
false
irb(main):017:0> x=66
66
irb(main):018:0> x>0 && x
66

Not to be misunderstood: I like this, but I’m not sure whether this is
guaranteed by the language spec.

Thx

robert

I use it a lot in this form:

a ||= "default"

i.e. ‘or’ evaluates to the left-hand operand if it’s not nil, otherwise it
evaluates to the right-hand operand.

So if it’s not guaranteed, then changing it would break a lot of my code :slight_smile:

Regards,

Brian.

···

On Wed, May 28, 2003 at 05:14:56PM +0900, Robert Klemme wrote:

is it guranteed that && and “and” return the last value if all values
before that return true? Consider:

irb(main):015:0> x=0
0
irb(main):016:0> x>0 && x
false
irb(main):017:0> x=66
66
irb(main):018:0> x>0 && x
66

Not to be misunderstood: I like this, but I’m not sure whether this is
guaranteed by the language spec.

Hi,

···

In message “Result of && and ‘and’” on 03/05/28, “Robert Klemme” bob.news@gmx.net writes:

irb(main):017:0> x=66
66
irb(main):018:0> x>0 && x
66

Not to be misunderstood: I like this, but I’m not sure whether this is
guaranteed by the language spec.

It’s guaranteed.

						matz.

So if it's not guaranteed, then changing it would break a lot of my code :slight_smile:

It's in the test for ruby

svg% less sample/test.rb
[...]

# make sure conditional operators work

test_check "assignment"

a=; a[0] ||= "bar";
test_ok(a[0] == "bar")
h={}; h["foo"] ||= "bar";
test_ok(h["foo"] == "bar")

aa = 5
aa ||= 25
test_ok(aa == 5)
bb ||= 25
test_ok(bb == 25)
cc &&=33
test_ok(cc == nil)
cc = 5
cc &&=44
test_ok(cc == 44)

[...]

svg%

Guy Decoux

“ts” decoux@moulon.inra.fr schrieb im Newsbeitrag
news:200305281018.h4SAIfV13245@moulon.inra.fr

So if it’s not guaranteed, then changing it would break a lot of my
code :slight_smile:

It’s in the test for ruby

Great! Thanks!

robert