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.
An 'in' operator for Ruby
11–20 of 69 posts
Re: An 'in' operator for Ruby
#12 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
#13Re: An 'in' operator for Ruby
#14[deleted]
Re: An 'in' operator for Ruby
#15I 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…
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
#16if 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.
Perhaps another way of doing this is calling the method .within?
Re: An 'in' operator for Ruby
#17Rails 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.
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
endRe: An 'in' operator for Ruby
#18> if ! (item in collection) # ok
Riiiiiiiight. No more language suggestions from you, thanks ;)
Re: An 'in' operator for Ruby
#19Ruby has a place for this sort of thing and they'll answer you.
Re: An 'in' operator for Ruby
#20Rails 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…