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.
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.
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.
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.
> 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 people love operators, that's why Ruby already includes loads of them instead of giving them identifier names and requiring you to call them with a dot. You have x+y instead of x.plus y, x==y instead of x.equals? y, x<y instead of x.less_than? y, most people write x..y instead of Range.new(x,y), etc. I don't think most Ruby programmers or the language designers agree with you about not wanting operators.
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.
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.
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?
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.
.include? isn't really a predicate, as predicates are typically unary. Rather, it's the 'member of' relational operator written in the wrong direction. Since we don't have '∈' in ascii, the author is proposing using the word 'in' instead.
I do disagree with the author on one point: 'not in' should also be a valid operator, representing '∉'.
But I don't use Ruby; I mostly use Python, which has these operators.
As for programming languages, I've always wondered how languages would've looked if computers, and as a result programming languages, had been invented in non-english speaking countries.
An immediate example that comes to mind is Python's use of None, rather than null (like in Java/JavaScript). I remember speaking in Dutch about Java in college was always slightly awkward, because it was easy to confuse "null" with 0 (or "nul"). I wonder if Guido (being Dutch) called the Python void type "None" on purpose, to avoid awkwardness when speaking about it in Dutch.
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.
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 :).
It doesn't work out very well in practice. Inevitably the syntax diverges, and then it's pretty confusing for the novice user: why do some sentences work but most don't?
[+] [-] tyre|10 years ago|reply
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.
[+] [-] jmcdonald-ut|10 years ago|reply
[+] [-] fleitz|10 years ago|reply
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
instead of[+] [-] nvader|10 years ago|reply
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.
[+] [-] bradhe|10 years ago|reply
[+] [-] guptaneil|10 years ago|reply
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.
[+] [-] Skoofoo|10 years ago|reply
> 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.
[+] [-] aetherson|10 years ago|reply
[+] [-] omaranto|10 years ago|reply
But people love operators, that's why Ruby already includes loads of them instead of giving them identifier names and requiring you to call them with a dot. You have x+y instead of x.plus y, x==y instead of x.equals? y, x<y instead of x.less_than? y, most people write x..y instead of Range.new(x,y), etc. I don't think most Ruby programmers or the language designers agree with you about not wanting operators.
[+] [-] golfadas|10 years ago|reply
[+] [-] Benjamin_Dobell|10 years ago|reply
> if ! (item in collection) # ok
Riiiiiiiight. No more language suggestions from you, thanks ;)
[+] [-] LouisSayers|10 years ago|reply
> 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.
[+] [-] unknown|10 years ago|reply
[deleted]
[+] [-] singularity2001|10 years ago|reply
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]
[+] [-] luiz-pv9|10 years ago|reply
[+] [-] kelseydh|10 years ago|reply
Perhaps another way of doing this is calling the method .within?
[+] [-] woodruffw|10 years ago|reply
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.
[+] [-] efaref|10 years ago|reply
I do disagree with the author on one point: 'not in' should also be a valid operator, representing '∉'.
But I don't use Ruby; I mostly use Python, which has these operators.
[+] [-] LouisSayers|10 years ago|reply
And as he points out, 'in' is already a keyword anyway - so if it makes a more readable way of doing things, then why not?
[+] [-] Phemist|10 years ago|reply
An immediate example that comes to mind is Python's use of None, rather than null (like in Java/JavaScript). I remember speaking in Dutch about Java in college was always slightly awkward, because it was easy to confuse "null" with 0 (or "nul"). I wonder if Guido (being Dutch) called the Python void type "None" on purpose, to avoid awkwardness when speaking about it in Dutch.
[+] [-] strong_code|10 years ago|reply
[+] [-] qewrffewqwfqew|10 years ago|reply
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 :).
[+] [-] AaronLasseigne|10 years ago|reply
Ruby has a place for this sort of thing and they'll answer you.
[+] [-] gkop|10 years ago|reply
[+] [-] VeejayRampay|10 years ago|reply
The fact that the Ruby method is called include? and not includes? is annoying though.
[+] [-] ishtu|10 years ago|reply
because it's Japanese https://www.new-bamboo.co.uk/blog/2010/12/17/learning-japane...
[+] [-] singularity2001|10 years ago|reply
[+] [-] pavlov|10 years ago|reply
https://en.wikipedia.org/wiki/AppleScript
It doesn't work out very well in practice. Inevitably the syntax diverges, and then it's pretty confusing for the novice user: why do some sentences work but most don't?
[+] [-] unknown|10 years ago|reply
[deleted]
[+] [-] u320|10 years ago|reply
[+] [-] unknown|10 years ago|reply
[deleted]
[+] [-] sargegood|10 years ago|reply
[+] [-] dorianm|10 years ago|reply
[+] [-] newobj|10 years ago|reply
[+] [-] VeejayRampay|10 years ago|reply
[+] [-] tom-lord|10 years ago|reply