Live data from Hacker News

An 'in' operator for Ruby

rubyhacker.com

11–20 of 69 posts

Re: An 'in' operator for Ruby

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

If we're talking about the natural language question, "Is Bob part of the Physics Department," then sometimes we conceptualize "groups" as more like tags. Like, Bob has a tag that says, "->physics-dept." And so we ask Bob if he has that tag. He doesn't need to know the entire list of people who also have that tag in order to answer that question authoritatively.

Re: An 'in' operator for Ruby

#12
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 like a zero-sum addition to the language to me.

Re: An 'in' operator for Ruby

#15

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 :).

Re: An 'in' operator for Ruby

#16
post #4

if x.in? my_set # very ugly I guess we disagree there. This is arguably more readable than most languages with `in?` being still just a method.

I agree, I quite like it as a method as it keeps its congruence with .include? as an opposite of .in? (what .in? does should have been what .include? did from the get go).

Perhaps another way of doing this is calling the method .within?

Re: An 'in' operator for Ruby

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

Then why have 2 * 4 when you can write 2.multiply_by 4...

What I love about ruby is its focus on making programming delightful rather than adhering to weird ass rules like 'Ruby is object oriented, meaning you are calling methods on objects.' that people pretend are laws of physics when really you can break them anytime you want, unlike laws of physics.

Similar to the way you can write

  def foo
    "bar"
  end 
instead of

  class Kernel
  
    def foo
      "bar"
    end
  
  end

Re: An 'in' operator for Ruby

#20
post #17
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.

Then why have 2 * 4 when you can write 2.multiply_by 4... What I love about ruby is its focus on making programming delightful rather than adhering to weird ass rules like 'Ruby is object oriented, meaning you are calling methods on objects.' that people pretend are laws of physics when really you can break them anytime you want, unlike laws of physics. Similar to the way you can write def foo "bar" end instead of cl…

[deleted]
Post reply on HN