Live data from Hacker News

An 'in' operator for Ruby

rubyhacker.com

31–40 of 69 posts

Re: An 'in' operator for Ruby

#33

> if item not in collection # travesty! > if ! (item in collection) # ok Riiiiiiiight. No more language suggestions from you, thanks ;)

Agreed > if item not in collection reads quite nicely. I don't see what the problem is there? Anyway, I don't think his one gripe with this is enough reason to throw the rest of his argument out.

unless item in collection

Obviously is the best choice

Re: An 'in' operator for Ruby

#34

> if item not in collection # travesty! > if ! (item in collection) # ok Riiiiiiiight. No more language suggestions from you, thanks ;)

Agreed > if item not in collection reads quite nicely. I don't see what the problem is there? Anyway, I don't think his one gripe with this is enough reason to throw the rest of his argument out.

[deleted]

Re: An 'in' operator for Ruby

#35

Earlier quoted context omitted.

Agreed > if item not in collection reads quite nicely. I don't see what the problem is there? Anyway, I don't think his one gripe with this is enough reason to throw the rest of his argument out.

unless item in collection Obviously is the best choice

Yes, and it's the idiomatic Ruby one.

Re: An 'in' operator for Ruby

#37
I actually found it more interesting that Ruby hashmaps ("hashes" :S) preserve insertion order and guarantee predictable insertion.

I was taught to never trust the ordering returned by iterating over keys of hashmaps because it can be a source of bugs as you move between architectures or interpreters. If you need a guaranteed ordering, keep (and pay for) the appropriate auxiliary data structure.

Has this lesson become obsolete? Perhaps "computers are fast" so now Ruby can afford to use ordered hashmaps everywhere. On the other hand, I know Go chose to randomize iteration order of keys on purpose so developers cannot rely on them.

Re: An 'in' operator for Ruby

#38

I understand the perceived benefit of compositional eloquence that this may add (pretty much borrowing from Python) but as others have pointed out, this is incongruent to the design of Ruby as a language and how it treats the relation between objects and their methods. Additionally, you lose any compositional elegance you may have gained when you decided not to use: x not in y and instead propose: !(x in y) Seems lik…

I find `not in` highly dubious - it makes the expression grammar awkward and harder to parse. Is `not in` a two-token operator? Or is `not` an adverb in this context? What else resembles this? To cite an alternatice, in Tcl, `in` is an operator and its negation is `ni`. A cute pun and echo of Monty Python that I'm disappointed Python didn't follow :).

You should be happy, as Python 3 would have renamed it to "Ekke Ekke Ekke Ekke Ptang Zoo Boing!"

As to the "what else resembles this"? I would turn it around and ask what else could resemble this. I'm not sure the conclusion would be that "not in" is a good thing, but not sure of the reverse, either:

  if x not = 3
looks weird, but I think I could grow to like it.

Re: An 'in' operator for Ruby

#39

> if item not in collection # travesty! > if ! (item in collection) # ok Riiiiiiiight. No more language suggestions from you, thanks ;)

>> if item not in collection

he should have suggested

if not item in collection

which should then work out of the box anyways

[I completely agree with the need for the 'in' operator]

Re: An 'in' operator for Ruby

#40
post #10

One of my favorite aspects of Ruby is that everything is an object with methods. Adding global operators/functions like this would only complicate the language. > But we also -- perhaps more often? -- ask questions like "Is this item part of this group?" We are asking a question about the item . Here we are actually asking the group a question about itself. An item would not know a group's contents.

Totally agree with you. I do think it is more natural to ask a collection if it has an element. On the other hand, it would be possible for Object to have an in? method that would receive a collection.
Post reply on HN