5 ms·
Regarding the @ positional-argument syntax: I mean, my use of Ruby dropped considerably years ago, but from a Ruby-philosophy standpoint it looks fantastic to m
by dasyatidprime 8y ago
Regarding the @ positional-argument syntax: I mean, my use of Ruby dropped considerably years ago, but from a Ruby-philosophy standpoint it looks fantastic to me. Having to constantly make up (usually single-character anyway) names for those parameters in map/filter chains was one of the ugliest parts of the language, especially when there was no actually-good name for things like temporary tuples, and the Smalltalk-derived (I think) syntax for block parameters is so visually heavyweight: { |…| … } { |…| … } { |…| … } where all the stuff in the || is close to useless, as opposed to just { … } { … } { … }. Method references passed with &, while separately useful, are not a good substitute; they're often less readable (to me) because the form of the expression is obscured, and they break down much faster, for instance as soon as you need any single other argument, rather than just when the block gets complex enough that the parameter names become important for readability.
I can appreciate that it moves farther away from “requiring things to be explicit means you get fewer accidental barriers to moving code around or increasing/decreasing complexity”, though, as some of my recent work has been in Java and I've had an eye for that aspect of it. But the next time I do anything in Ruby I'll be remembering that this is there.
- vbsteven 8y agoI like Kotlins solution for this. If there is only one argument in the lambda/block and the type is clear from the context you don’t have to declare it and you can use the ‘it’ keyword instead.
- pkulak 8y agoAnd one parameter is 99% of the cases. If you have more than one, you should probably name them anyway.
- dragonwriter 8y ago> And one parameter is 99% of the cases. In Ruby, the two-parameter hash-iteration case alone is common enough that that's not true. > If you have more than one, you should probably name them anyway. In a single line block, I think that often impairs rather than enhances readability.
- pkulak 8y agok, v impairs readability?
- vbsteven 8y agoFor clarification, it looks like this people.filter { it.age > 42 }
- majewsky 8y agoNeeds more aliases: things.filter { its.age > 42 } people.filter { their.age > 42 } ;)
- deleted 8y ago[deleted]
- skrebbel 8y agoWow, that's splendid! In a sense, it's a very nice dual to "this", which is also a keyword everywhere and not something like $0 or something. I wish more languages had this. Or, it.
- ricardobeat 8y agoIf you go back and read the post and linked ticket, that is indeed the original proposal for Ruby, which somehow morphed into positional arguments.
- jimmychangas 8y agoKotlin is great, but I think the "it" syntax was derived from Groovy.
- zbentley 8y agoI'm not positive, but Groovy may in turn have been inspired by Perl's $_, as in 'map { $_ + 1 } (1..10);'. If I recall correctly, the canonical name for that parameter is 'it' as well (though I've never met a Perl programmer that calls it that . . . er, calls it 'it' . . . er, you get the idea).
- dchest 8y agoThe first time I saw `it` in a similar construction was in Arc language's aif macro: (mac aif (expr . body) `(let it ,expr (if it ,@(if (cddr body) `(,(car body) (aif ,@(cdr body))) body)))) HN uses it quite a lot, e.g.: (defop logout req (aif (get-user req) (do (logout-user it) (pr "Logged out.")) (pr "You were not logged in.")))
- bjoli 8y agoAnaphoric macros have been used in common lisp since forever.
- dchest 8y agoThanks for the name! Here's a wikipedia article about them: https://en.wikipedia.org/wiki/Anaphoric_macro https://en.wikipedia.org/wiki/Anaphoric_macro
- emidln 8y agoPGs On Lisp details these macros from common lisp. I'm unsure how old they are.
- zbentley 8y ago
- zimbatm 8y agoRuby already has perl-like magic variables. Too bad $1 $2.. are already used to hold regexp matches if I remember correctly. I suspect they could have introduced $it for single-variable blocks and not break the existing parsers.
- vidarh 8y ago$ signifies a global variable, so changes are $it would break some existing codebase somewhere.
- Dirlewanger 8y agoThankfully, it's heavily frowned-upon to use global variables.