Live data from Hacker News

An 'in' operator for Ruby

rubyhacker.com

21–30 of 69 posts

Re: An 'in' operator for Ruby

#21
post #20
post #17

Earlier quoted context omitted.

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]

Smalltalk messages/methods:

   a in: set.
   2 * 4.
Just sayin'

Re: An 'in' operator for Ruby

#22
post #20
post #17

Earlier quoted context omitted.

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]

[deleted]

Re: An 'in' operator for Ruby

#23

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

Another alternative is the way D does it. Its the same idea as `not in` but instead of adding a 2 keyword operator and non-standard usage of negation (`not` instead of `!`) it is simply: `!in`

Re: An 'in' operator for Ruby

#25
post #24

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

[deleted]

I believe the parent comment is agreeing with you (?) and disagreeing with the author of the linked article.

Re: An 'in' operator for Ruby

#26

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

Re: An 'in' operator for Ruby

#27

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

I actually find that the use of "!" over "not" improves readability. English words tend to all blend together and take more time to parse, whereas a nice "!" token is easy to recognize at a glance. That being said, "not" is of course more understandable for somebody who has no idea about the language; however, asking a programmer to learn a single symbol for "not" is not too much to ask :)

Re: An 'in' operator for Ruby

#28

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.

I don't think it 'goes against' anything... it's simply a more concise way of doing the .include? - it's syntactic sugar.

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?

Re: An 'in' operator for Ruby

#29

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.

I actually find that the use of "!" over "not" improves readability. English words tend to all blend together and take more time to parse, whereas a nice "!" token is easy to recognize at a glance. That being said, "not" is of course more understandable for somebody who has no idea about the language; however, asking a programmer to learn a single symbol for "not" is not too much to ask :)

depends how much attention you're paying at the moment.

its easier to miss a ! than a 'not' at a quick glance

Re: An 'in' operator for Ruby

#30
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…

The difference here is that `` literally maps onto a method called `` in Ruby. I find this aspect of Ruby delightful and it's not something I willingly chip away at.

Generally speaking, Ruby eschews syntax that invokes a protocol behind the scenes for a consistent 1:1 mapping between method syntax and the methods they invoke.

Post reply on HN