From: "Mariusz Pękala" Date: 2007-06-21T18:54:09+09:00 Subject: Re: if == any item in an array --gKMricLos+KVdGMg Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2007-06-21 09:17:24 +0900 (Thu, Jun), 12 34 wrote: > > Give this code a shot. You almost had it -- the syntax you were > > looking for was the leading splat "*" in front of the PhotoEndings and > > MovieEndings in the branches of the case statement. > >=20 > Thank you and all the others that answered. The array.include was what I= =20 > was thinking too. But removing a couple of commas and adding a couple of= =20 > astericks (splats) is nice. Ruby is great. >=20 > I don't understand the meaning of the *. I imagine it comes from the=20 > wildcard usage. What do I look up in my books to understand this. When the splat-solution appeared here an alarm bell rung in my head: If the array is small, like in this problem, everything is OK. But what if the array is huge? I was wondering whether stack overflows could occur. I havent found them, but the benchmark shows that .include?() is faster. require 'benchmark' LIMIT =3D 10_000_000 STEP =3D LIMIT / 10 + 1 BIG_ARRAY =3D (1..LIMIT).to_a; nil def look_by_splash(item) case item when *BIG_ARRAY 'found by splash' else 'not found by splash' end end def look_by_include(item) case when BIG_ARRAY.include?(item) 'found by include' else 'not found by include' end end Benchmark.bmbm do |x| x.report('by_splash') do 1.step(LIMIT + STEP, STEP) { |i| look_by_splash(i) } end x.report('by_include') do 1.step(LIMIT + STEP, STEP) { |i| look_by_include(i) } end end Rehearsal --------------------------------------------- by_splash 13.080000 0.000000 13.080000 ( 13.212631) by_include 6.750000 0.000000 6.750000 ( 6.817161) ----------------------------------- total: 19.830000sec user system total real by_splash 12.930000 0.000000 12.930000 ( 13.060550) by_include 6.750000 0.000000 6.750000 ( 6.809274) I tested with LIMIT =3D 100_000_000, and the proportions are the same. --=20 Ceterum censeo Internet Explorer esse delendam. --gKMricLos+KVdGMg Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7-ecc0.1.6 (GNU/Linux) iD8DBQFGekrAsnU0scoWZKARAhOnAKDApJD6zJDnIXcDjrQu6eYhr+wNvwCcDHvY sdEQycx8P27MYOOsQn9R54M= =qmqg -----END PGP SIGNATURE----- --gKMricLos+KVdGMg--