[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"num in [1,2,3,4]" in a cool way?

Iñaki Baz Castillo

8/20/2008 9:33:00 PM

Hi, AFAIK in Ruby the only (or the "coolest") way to do something as:

if num in [1,2,3,4]

is by doing:

if [1,2,3,4].index(num)

Is it? any other "cooler" way? Thanks.

=2D-=20
I=C3=B1aki Baz Castillo

16 Answers

Rob Biedenharn

8/20/2008 9:41:00 PM

0

On Aug 20, 2008, at 5:32 PM, I=F1aki Baz Castillo wrote:

> Hi, AFAIK in Ruby the only (or the "coolest") way to do something as:
>
> if num in [1,2,3,4]
>
> is by doing:
>
> if [1,2,3,4].index(num)
>
> Is it? any other "cooler" way? Thanks.
>
> -- =20
> I=F1aki Baz Castillo

if [1,2,3,4].include?(num)

if (1..4) =3D=3D=3D num

Robert might chime in with an inject version, so I won't do one of =20
those.

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Stefano Crocco

8/20/2008 9:43:00 PM

0

On Wednesday 20 August 2008, I=C3=B1aki Baz Castillo wrote:
> Hi, AFAIK in Ruby the only (or the "coolest") way to do something as:
>
> if num in [1,2,3,4]
>
> is by doing:
>
> if [1,2,3,4].index(num)
>
> Is it? any other "cooler" way? Thanks.

if [1,2,3,4].include? num

It's not that different, but it looks more like English.

Stefano


Iñaki Baz Castillo

8/20/2008 9:52:00 PM

0

El Mi=E9rcoles, 20 de Agosto de 2008, Rob Biedenharn escribi=F3:
> if [1,2,3,4].include?(num)
>
> if (1..4) =3D=3D=3D num

Thanks.


=2D-=20
I=F1aki Baz Castillo

Martin DeMello

8/20/2008 10:08:00 PM

0

On Wed, Aug 20, 2008 at 2:32 PM, I=F1aki Baz Castillo <ibc@aliax.net> wrote=
:
> Hi, AFAIK in Ruby the only (or the "coolest") way to do something as:
>
> if num in [1,2,3,4]
>
> is by doing:
>
> if [1,2,3,4].index(num)
>
> Is it? any other "cooler" way? Thanks.


class Object
def in? (other)
other.respond_to?("include?") && other.include?(self)
end
end

num.in? [1,2,3,4]

martin

Frederick Cheung

8/21/2008 1:24:00 AM

0


On 20 Aug 2008, at 22:32, I=F1aki Baz Castillo wrote:

> Hi, AFAIK in Ruby the only (or the "coolest") way to do something as:
>
> if num in [1,2,3,4]
>
> is by doing:
>
> if [1,2,3,4].index(num)
>
> Is it? any other "cooler" way? Thanks.


Well not saying this is actually a good idea, but...
class Object
def in(collection)
collection.include? self
end
end

if num.in [1,2,3,4]
...
end=

Michael Fellinger

8/21/2008 2:06:00 AM

0

T24gVGh1LCBBdWcgMjEsIDIwMDggYXQgNjo0MSBBTSwgUm9iIEJpZWRlbmhhcm4KPFJvYkBhZ2ls
ZWNvbnN1bHRpbmdsbGMuY29tPiB3cm90ZToKPiBPbiBBdWcgMjAsIDIwMDgsIGF0IDU6MzIgUE0s
IEnDsWFraSBCYXogQ2FzdGlsbG8gd3JvdGU6Cj4KPj4gSGksIEFGQUlLIGluIFJ1YnkgdGhlIG9u
bHkgKG9yIHRoZSAiY29vbGVzdCIpIHdheSB0byBkbyBzb21ldGhpbmcgYXM6Cj4+Cj4+ICBpZiBu
dW0gaW4gWzEsMiwzLDRdCj4+Cj4+IGlzIGJ5IGRvaW5nOgo+Pgo+PiAgaWYgWzEsMiwzLDRdLmlu
ZGV4KG51bSkKPj4KPj4gSXMgaXQ/IGFueSBvdGhlciAiY29vbGVyIiB3YXk/IFRoYW5rcy4KPj4K
Pj4gLS0gScOxYWtpIEJheiBDYXN0aWxsbwo+Cj4gaWYgWzEsMiwzLDRdLmluY2x1ZGU/KG51bSkK
Pgo+IGlmICgxLi40KSA9PT0gbnVtCj4KPiBSb2JlcnQgbWlnaHQgY2hpbWUgaW4gd2l0aCBhbiBp
bmplY3QgdmVyc2lvbiwgc28gSSB3b24ndCBkbyBvbmUgb2YgdGhvc2UuCgpudW0gPSA1ClsqMS4u
NF0uaW5qZWN0KG51bSl7fG0sdnwgbSA9PSB0cnVlID8gbSA6IChtID09IHYgfHwgbSkgfSA9PSB0
cnVlCgpCdXQgaXQncyBmYXIgZnJvbSBjb29sLCBhbmQgbXkgbmFtZSBpc24ndCBSb2JlcnQsIGJ1
dCBpIGhvcGUgaXQgd2FrZXMKdXAgdGhlIGNvbXBldGl0aXZlIHNwaXJpdCBvZiBhbGwgaW5qZWN0
aW9uYWxpc2l0cy4KCl4gbWFudmVydQo=

Martin DeMello

8/21/2008 2:59:00 AM

0

On Wed, Aug 20, 2008 at 7:05 PM, Michael Fellinger
<m.fellinger@gmail.com> wrote:
>
> num = 5
> [*1..4].inject(num){|m,v| m == true ? m : (m == v || m) } == true

ary.inject([num, false]) {|(n,a), i| [n, a | (n == i)]}.last

martin

Mikael Høilund

8/21/2008 3:04:00 AM

0


On Aug 21, 2008, at 4:58, Martin DeMello wrote:

> On Wed, Aug 20, 2008 at 7:05 PM, Michael Fellinger
> <m.fellinger@gmail.com> wrote:
>>
>> num =3D 5
>> [*1..4].inject(num){|m,v| m =3D=3D true ? m : (m =3D=3D v || m) } =3D=3D=
true
>
> ary.inject([num, false]) {|(n,a), i| [n, a | (n =3D=3D i)]}.last
>

num =3D 5
ary.inject(false) { |m, v| true if m or v =3D=3D num }


--=20
Name =3D "Mikael H=F8ilund"; Email =3D Name.gsub %r/\s/,%#=3D?,#
*a=3De=3D?=3D,!????,:??,?,,Email.downcase![eval(%["\\%o\\%o"]%
[?**2+?o,?\\*2])]=3D"o";Email.gsub! %%\%c%*3%a, %?%c? % ?@
def The(s)%%\%s.%%s+%.org\n.end; :Go and print The Email


Martin DeMello

8/21/2008 3:08:00 AM

0

On Wed, Aug 20, 2008 at 8:03 PM, Mikael H=F8ilund <mikael@hoilund.org> wrot=
e:
>
> num =3D 5
> ary.inject(false) { |m, v| true if m or v =3D=3D num }

or even

ary.inject(false) { |m, v| m or v =3D=3D num }

Robert Klemme

8/21/2008 11:38:00 AM

0

2008/8/21 Martin DeMello <martindemello@gmail.com>:
> On Wed, Aug 20, 2008 at 8:03 PM, Mikael H=F8ilund <mikael@hoilund.org> wr=
ote:
>>
>> num =3D 5
>> ary.inject(false) { |m, v| true if m or v =3D=3D num }
>
> or even
>
> ary.inject(false) { |m, v| m or v =3D=3D num }

Disclaimer: I would not use #inject in this case.

But since you asked...

We can even short circuit with #inject:

irb(main):003:0> i =3D 3
=3D> 3
irb(main):004:0> (1..4).inject(nil) {|b,a| break a if i =3D=3D a}
=3D> 3
irb(main):005:0> i =3D 10
=3D> 10
irb(main):006:0> (1..4).inject(nil) {|b,a| break a if i =3D=3D a}
=3D> nil

But then again, you could directly use #find or even better #include?.

If it's for integers I'd probably just do

irb(main):010:0> i =3D 3
=3D> 3
irb(main):011:0> i >=3D 1 && i <=3D 4
=3D> true
irb(main):012:0> (1..4) =3D=3D=3D i
=3D> true

Here's a different approach:

irb(main):013:0> pat =3D (1..4).inject(0) {|x,a| x | 1 << a}
=3D> 30
irb(main):014:0> pat.to_s 2
=3D> "11110"
irb(main):015:0> pat[i] =3D=3D 1
=3D> true

:-)

Kind regards

robert

--=20
use.inject do |as, often| as.you_can - without end