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

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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
 
R

Rob Biedenharn

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://agileconsultingllc.com
(e-mail address removed)
 
S

Stefano Crocco

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

Iñaki Baz Castillo

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
 
M

Martin DeMello

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
 
F

Frederick Cheung

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=
 
M

Michael Fellinger

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

Mikael Høilund

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
 
R

Robert Klemme

2008/8/21 Martin DeMello said:
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 =3D=3D 1
=3D> true

:)

Kind regards

robert

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

Robert Dober

On Thu, Aug 21, 2008 at 1:38 PM, Robert Klemme
But then again, you could directly use #find or even better #include?.

In case of ranges #include? really should be your choice, it seems to
be optimized as one
might have expected....


511/12 > cat find-include.rb && ruby find-include.rb
# vim: sw=2 ts=2 ft=ruby expandtab tw=0 nu:

require 'benchmark'


R = (1..1_000)
N = 5
Benchmark::bmbm do |bm|
bm.report("find") do
N.times do
R.each do |ele|
R.find{ |e| e == ele}
R.find{ |e| e.zero? }
end
end
end
bm.report("include?") do
N.times do
R.each do |ele|
R.include? ele
R.include? 0
end
end
end
end
Rehearsal --------------------------------------------
find 6.797000 0.000000 6.797000 ( 6.828000)
include? 0.000000 0.000000 0.000000 ( 0.016000)
----------------------------------- total: 6.797000sec

user system total real
find 6.438000 0.000000 6.438000 ( 6.828000)
include? 0.016000 0.000000 0.016000 ( 0.016000)
HTH
Robertendless loops might run for quite some time though ;)
 
I

Iñaki Baz Castillo

El Jueves, 21 de Agosto de 2008, Gregory Brown escribi=F3:
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.

num.between?(1,4)

Great, I didn't know! :)

But in my case I'm using custom objects instead of Fixnum or String.

Thanks a lot.

=2D-=20
I=F1aki Baz Castillo
 
G

Gregory Brown

El Jueves, 21 de Agosto de 2008, Gregory Brown escribi=F3:
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.

num.between?(1,4)

Great, I didn't know! :)

But in my case I'm using custom objects instead of Fixnum or String.

Sure, this is implemented by Comparable, so you just need to include
that module and implement <=3D>

--=20
Technical Blaag at: http://blog.majesticseacreature.com | Non-tech
stuff at: http://metametta.blogspot.com
 
P

Peña, Botp

From: I=F1aki Baz Castillo [mailto:[email protected]]=20
# > num.between?(1,4)
#=20
# Great, I didn't know! :)

careful. it just test the boundaries.
2.5.between?(1,4)
=3D> true
[1,2,3,4].include? 2.5
=3D> false
system "qri between"
---------------------------------------------------- Comparable#between?
obj.between?(min, max) =3D> true or false
------------------------------------------------------------------------
Returns false if obj <=3D> min is less than zero or if anObject =
<=3D>
max is greater than zero, true otherwise.

3.between?(1, 5) #=3D> true
6.between?(1, 5) #=3D> false
'cat'.between?('ant', 'dog') #=3D> true
'gnu'.between?('ant', 'dog') #=3D> false


kind regards -botp
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top