Phrogz
7/27/2007 10:10:00 PM
On Jul 26, 5:38 pm, dbl...@rubypal.com wrote:
> On Fri, 27 Jul 2007, Phrogz wrote:
> > On Jul 26, 4:22 pm, Brett Boge <brett.b...@igt.com> wrote:
> >> 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
> 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.)