n00b if condition quest

B

Brett Boge

I'm sure theres a way, so how does one do:

if (a = b or a = c or a = d or a = f)

in a shorter, easier to view

if (a = (b c d or f)) kind of way?
 
D

dblack

P

Phrogz

I'm sure theres a way, so how does one do:

if (a = b or a = c or a = d or a = f)

in a shorter, easier to view

if (a = (b c d or f)) kind of way?

case n
when 1,2,3: puts '1-3'
when 4..6: puts '4-6'
else puts 'other'
end

You can, of course, use variables instead of literals, ala

case a
when b, c, d, f
...
end
 
P

Peña, Botp

T24gQmVoYWxmIE9mIEJyZXR0IEJvZ2U6DQojID09IGluZGVlZC4NCg0KYWxzbyB0YWtlIGEgbG9v
ayBhdCAjYW55PyBhbmQgI2FsbD8NCg0KdGhleSBjb21lIGluIGhhbmR5IGluIHNpdHVhdGlvbnMg
bGlrZSwNCg0KaWYgYSA8IGIgb3IgYSA8IGMgb3IgYSA8IGQNCg0KaWYgYSA+IGIgYW5kIGEgPiBj
IGFuZCBhID4gZA0KDQpzb21ldGltZXMsIHlvdSBtYXkgd2FudCBhIHJldmVyc2UgdG8gI2luY2x1
ZGU/IGJlaGF2aW9yIHNvIHRoYXQgeW91J2QgbGlrZSB0byBlbXBoYXNpemUgZmlyc3QgdGhlIGVs
ZW1lbnQgYXMgY29tcGFyZWQgdG8gdGhlIGNvbGxlY3Rpb24uIHNvbWV0aGluZyBsaWtlLA0KDQph
LmluP1tiLGMsZCxmXQ0KDQoNCmtpbmQgcmVnYXJkcyAtYm90cA0K
 
R

Robert Dober

Hi --

I'm sure theres a way, so how does one do:

if (a = b or a = c or a = d or a = f)

in a shorter, easier to view

if (a = (b c d or f)) kind of way?

You mean == rather than = , but in any case, try this:

if [b,c,d,f].include?(a)

You are right for sure that OP meant ==, but let us answer the
original question too ;)

a = [b,c,d,e,f].compact.first

Robert
 
I

Ian Whitlock

Robert said:
You are right for sure that OP meant ==, but let us answer the
original question too ;)

a = [b,c,d,e,f].compact.first

Not quite.

b=c=d=false
e=f = 99
a1 = [b,c,d,e,f].compact.first
a2=b or a2=c or a2=d or a2=e or a2=f
puts a1, a2

a1 is false and a2 is 99

Ian
 
P

Phrogz

That doesn't test for equality, though.

An important semantic point. For example:

case 1..3
when 1..3: puts 'yay!'
else puts 'boo'
end

results in "boo", because (1..3) === (1..3) #=> false

Still, as the docs for Object#=== say:
"For class Object, effectively the same as calling #==, but typically
overridden by descendants to provide meaningful semantics in case
statements."

Numbers, strings, arrays, hashes, booleans...all these treat === as
==. I'm not arguing that they should be treated the same, or that we
should sweep the difference under the rug. I'm simply suggesting that
if you know the difference between #== and #===, particularly on the
objects that you place in your case statements, then under many
circumstances you can use a case statement as a convenience for
checking equality on many objects at once.

(Not that there was anything wrong with your initial suggestion of
Array#any?, of course.)
 
P

Phrogz

You mean == rather than = , but in any case, try this:
You are right for sure that OP meant ==, but let us answer the
original question too ;)

a = [b,c,d,e,f].compact.first

Your solution forces the evaluation of b/c/d/e/f, which the OP's does
not (thanks to the miracle of short-circuit boolean evaluation). I
think the 'correct' answer to the typo-incorrect question is:

if a = (b or c or d or f)

To be super clear: this is only because we're talking about assignment
instead of an actual equality test.
 

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

Forum statistics

Threads
474,264
Messages
2,571,314
Members
47,993
Latest member
Meri511146

Latest Threads

Top