Live data from Hacker News

An 'in' operator for Ruby

rubyhacker.com

1–10 of 69 posts

Re: An 'in' operator for Ruby

#3
Rails implements this as `in?`

https://github.com/rails/rails/blob/d61baee52adcd1baebe15f10...

You're welcome to try and merge that into Ruby core itself. That said, "I like whitespace" isn't a good reason to add a new operator over a method. Ruby is object oriented, meaning you are calling methods on objects.

Re: An 'in' operator for Ruby

#5
post #3

Rails implements this as `in?` https://github.com/rails/rails/blob/d61baee52adcd1baebe15f10... You're welcome to try and merge that into Ruby core itself. That said, "I like whitespace" isn't a good reason to add a new operator over a method. Ruby is object oriented, meaning you are calling methods on objects.

He addresses this in his post.

  if x.in? my_set   # very ugly
With that said, I personally don't agree with the post.

Re: An 'in' operator for Ruby

#6
When I was first learning Ruby, having to invert my natural thought process in order to use `include?` was one of the most frustrating things I had to get used to. I think that's more a testament to how amazing Ruby is at imitating natural language patterns when that was my biggest complaint.

Having said that, I don't agree that it needs to be an operator over a method. I agree that it would be prettier, but methods provide additional benefits like being easy to override or pass as arguments to other methods (like :respond_to?). If you're ok with a method, you can easily monkey patch ruby to support an `in` (or `in?`) method yourself.

Re: An 'in' operator for Ruby

#7
This would go against the ruby style of using predicates for this sort of thing (think `include?`, `empty?`, `nil?`).

I personally wouldn't be in favor of it.

Addition: Arguing that "Ruby isn't English" and then stating conformity to mathematical notation as a benefit seems hypocritical. Programming languages (besides APL, maybe) do not use strict mathematical notation, and we shouldn't want them to.

Re: An 'in' operator for Ruby

#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.

Post reply on HN