using "if 3 in list_foo" for Python for Ruby

  • Thread starter SpringFlowers AutumnMoon
  • Start date
S

SpringFlowers AutumnMoon

There is no such thing as

if 3 in [3, 2, 1]

construct in Ruby? so the common way in Ruby is

if list1.include?(3)

or

if list1.index(3)

is that right? is there an idiom in Ruby to do something like

if 3.in([3,2,1])

or

if 3.in [3,2,1]
 
D

David A. Black

Hi --

There is no such thing as

if 3 in [3, 2, 1]

construct in Ruby? so the common way in Ruby is

if list1.include?(3)

or

if list1.index(3)

is that right? is there an idiom in Ruby to do something like

if 3.in([3,2,1])

or

if 3.in [3,2,1]

The include? way is the usual way. Hal Fulton at one point proposed an
in or in? method that would essentially be:

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

but I don't think it's on the radar for being added.


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!
 
S

SpringFlowers AutumnMoon

David said:
if 3.in [3,2,1]

The include? way is the usual way. Hal Fulton at one point proposed an
in or in? method that would essentially be:

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

but I don't think it's on the radar for being added.


Hm... wouldn't that be handy...

i just sort of think that, if you ask me, "have you been to [Hawaii,
China, France]", i probably should be able to look at each one and tell
you an answer of yes or no, instead of asking [Hawaii, China, France] to
tell whether they have seen me.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top