5 ms·
Ruby now has “punning.” > Values in Hash literals and keyword arguments can be omitted. [Feature #14579] > {x:, y:} is a syntax sugar of {x: x, y: y}. > foo(
by burlesona 5y ago
Ruby now has “punning.”
> Values in Hash literals and keyword arguments can be omitted. [Feature #14579]
> {x:, y:} is a syntax sugar of {x: x, y: y}.
> foo(x:, y:) is a syntax sugar of foo(x: x, y: y).
This is going to be controversial and take some getting used to, but I think eventually everyone will love it. It’s not an easy syntax to learn - I remember when this came to JS it took a while to get my head around all the different ways this shorthand was used in the wild.
It’s hard to come up with new syntax that can peacefully coexist with the established language. I think in hindsight it would have been helpful for Ruby if it had done something like square braces instead of brackets for hash literals, or not allowed brackets for blocks. The most ambiguous syntax all involves hash literals vs blocks… this adds to that cognitive load.
But typing out a long hash literal just to capture a bunch of local variables or methods is so common, and the duplication is quite annoying, so on the whole I’m glad this was added and am sure I’ll use it a lot :)
- codesnik 5y ago"everyone wants the colon" / Larry Wall. perfect example.
- codesnik 5y agoI wonder, will there be a breaking change for older syntax, where it was previously foo x: [1,2,3] Or maybe that "punning" will work only inside of () and {}?
- jaynetics 5y agoOf course cases where parentheses are omitted will keep working like before, changing this would silently affect huge amounts of code. See also https://zverok.github.io/blog/2021-12-08-value-omission-debug.html https://zverok.github.io/blog/2021-12-08-value-omission-debu...
- sodapopcan 5y agoI'll throw myself out there to say that after being forced to read punning in JS for the past 2+ years, I've never come around to it. Sure it's quicker (and I'll admit even nicer) to write, but even after seeing it daily for so long, I still find myself doing double-takes on what I'm looking at, and I need as little as possible of that while reading code. I'm a little disappointed that Ruby introduced it but no one is forcing me to use it so to each their own (and I'm not writing Ruby or much JS professionally anymore so it doesn't affect me too much).
- treis 5y ago>This is going to be controversial and take some getting used to, but I think eventually everyone will love it It's saving a single character in your example. I can see liking it as solving a minor inconvenience. But love? I don't see that. To me it's just another thing to learn.
- nocman 5y agoMaybe so, for some, but I find: {width:, height:, x_pos:, y_pos:} to be much more pleasant than {width: width, height: height, x_pos: x_pos, y_pos: y_pos} And the more items you add the more I find it useful. Yeah, it took some getting used to in Javascript after years and years without it, but I personally would not want to go back to not having it. I'm not crazy about the colon/comma combo, but if I was writing in Ruby I suspect I'd get used to it after a while.
- ljm 5y agoThe curly braces in Ruby do an awful lot of work (blocks, procs, lambdas, hashes, interpolation) and I wonder if part of it is because it helped bring Smalltalk and Lisp-related concepts to people who were mostly familiar with C-style languages. Arguably it was the right call considering where Ruby is now.
- zarzavat 5y agoThis feature has such a positive effect on code quality in JS. It makes it easier to give consistent names to variables, and harder to use inconsistent names. You really notice when you write { foo: bar } instead of { foo } and it becomes somewhat of a code smell. Could variable bar be changed to foo? If yes then great, that's one less naming variation you have to remember for 'foo'. Across a whole codebase that can be a significant improvement.
- mpd 5y agoMy question is whether or not rubocop can autocorrect it to whatever. If so, I really dgaf about this.
- dopeboy 5y agoI've always wondered what this feature was called and never knew how to Google for it. TIL - thank you.