Handy method Object#in?

K

Kamil

I wish there would be this simple method in the core:

class Object
def in?(an_array)
an_array.include?(self)
end
end

Having that it's nice to write:

a = %w(hello world out there)
puts 'world'.in?(a)

And it works with Interval instance too.
 
D

David A. Black

Hi --

I wish there would be this simple method in the core:

class Object
def in?(an_array)
an_array.include?(self)
end
end

Having that it's nice to write:

a = %w(hello world out there)
puts 'world'.in?(a)

And it works with Interval instance too.

This is a bit of a perma-thread on this mailing list. I guess it's
hard to Google for :) But it's been talked about a lot.


David
 
T

Trans

I wish there would be this simple method in the core:

class Object
def in?(an_array)
an_array.include?(self)
end
end

Having that it's nice to write:

a = %w(hello world out there)
puts 'world'.in?(a)

concise, but it inverts the oop flow. is it really a big deal to do:

a.include?('world')

t.
 
P

Pena, Botp

From: Trans [mailto:[email protected]]=20
# > I wish there would be this simple method in the core:
# > class Object
# > def in?(an_array)
# > an_array.include?(self)
# > end
# > end
# > Having that it's nice to write:
# > a =3D %w(hello world out there)
# > puts 'world'.in?(a)
# concise, but it inverts the oop flow. is it really a big deal to do:
# a.include?('world')

i'm not sure what you mean by oop flow, but i use it like,

obj=3D"world"
array =3D %w(hello world out there)

obj.method if obj in? array
=20
in english eg, i'd say

john will swim if he is on the swimming team.

not

john will swim if the swimming team includes him (??) [ or replace =
include w other relevant words]

but yes, that is english, so in ruby we prefer the latter? (just =
teasing ;-)

in fact, i would even like to extend #in? to return the position (if =
array) or pair (if hash) of obj in collection (nil otherwise); currently =
include?only returns plain true/false.

kind regards -botp
=20
ps: heheh, note that i'm also using #in in facets among other things :) =
why (in ruby) can't an object ask itself if it's a member of a =
collection??
 
T

Trans

From: Trans [mailto:[email protected]]
# > I wish there would be this simple method in the core:
# > class Object
# > def in?(an_array)
# > an_array.include?(self)
# > end
# > end
# > Having that it's nice to write:
# > a = %w(hello world out there)
# > puts 'world'.in?(a)
# concise, but it inverts the oop flow. is it really a big deal to do:
# a.include?('world')

i'm not sure what you mean by oop flow, but i use it like,

obj="world"
array = %w(hello world out there)

obj.method if obj in? array

in english eg, i'd say

john will swim if he is on the swimming team.

not

john will swim if the swimming team includes him (??) [ or replace include w other relevant words]

but yes, that is english, so in ruby we prefer the latter? (just teasing ;-)

true enough, computer language tend to force different order though.
try to imagine it forth ;)
in fact, i would even like to extend #in? to return the position (if array) or pair (if hash) of obj in collection (nil otherwise); currently include?only returns plain true/false.

not a bad idea.
kind regards -botp

ps: heheh, note that i'm also using #in in facets among other things :) why (in ruby) can't an object ask itself if it's a member of a collection??

Haha! yep. you got me ;) but i think its fine for add-on, i mean Ruby
can't do everything on it's own, can it ;)

the reason ruby itself should not, is because it adds a method to all
objects that simply inverts the actual oop order. techincally we could
do that with just about every method.

a.index(e)
e.index_of(a)

h.key?(k)
k.key_of?(h)

etc.

Now this reminds me though, why don't we have String#puts ?

t.
 
S

Sean O'Halpin

I wish there would be this simple method in the core:

class Object
def in?(an_array)
an_array.include?(self)
end
end

Having that it's nice to write:

a = %w(hello world out there)
puts 'world'.in?(a)

And it works with Interval instance too.

It is nice, but I would advise you to profile code that uses it and
ask yourself if it is worth the cost.

Regards,
Sean
 

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
474,270
Messages
2,571,352
Members
48,035
Latest member
SvenDriver

Latest Threads

Top