From: blake.h.l.west@... Date: 2018-11-02T20:29:10+00:00 Subject: [ruby-core:89682] [Ruby trunk Feature#15236] add support for hash shorthand Issue #15236 has been updated by blakewest (Blake West). Hi all, I've really wanted a feature like this for a long time. I find myself often using named arguments, which I love for the clarity. But then the callers often have variables of the same name as the parameter name, making it more verbose. eg. `foo(bar: bar, baz: baz)`. If one concern here is that `{a, b, c}` looks like set syntax, then I'd be curious to get opinions on a different syntax I had thought about for a while. The general idea is to extend the `%w` idea for hashes. For example, we could do `%h{foo bar baz}` which would expand into `{foo: foo, bar: bar, baz: baz}`. In both the "%w" and "%h" cases, we're allowing for a more concise version of an often used pattern. I think the downside is using a `%h` syntax feels "separated" from the typical hash syntax. Using a straight `{a,b,c}` is "cleaner" as there are fewer characters, and may be more intuitive to some. But similarly, I think that actually the upside of this syntax is it's more "separated". As Matz points out, the `{a,b,c}` syntax could look like set syntax, and as shevegen points out, it could be confusing for beginners. Neither of these problems would exist using %h, as it would pretty clearly be a "special" syntax, used by people who know what it's doing. Also, using `%h{}` would enable not having to use commas, which could make it even more compact I'll leave with a few more examples of what I'd be suggesting. ~~~ ruby def foo(param1:, param2:) param + param2 end param1 = 7 param2 = 42 foo(%h{param1 param2}) def respond_with(resource, options) meta = extract_meta(resource, options) etc = extract_etc(resource, options) %h{ resource meta etc } end # destructuring could obviously be left for later. %h{data meta etc} = {data: [1,2,3], meta: {mobile: true}, etc: "more info"} ~~~ Thoughts? ---------------------------------------- Feature #15236: add support for hash shorthand https://bugs.ruby-lang.org/issues/15236#change-74722 * Author: ignatiusreza (Ignatius Reza Lesmana) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- PR in github: https://github.com/ruby/ruby/pull/1990 inspired by javascript support for object literal shorthand notation `{ a }`, which will be expanded into `{ a: a }`.. to avoid ambiguity, this shorthand is only supported when hash is defined with `{ }` notation.. in other situation where the brackets is optional, e.g. function call, we still need to write it in full (`m(a : a)` instead of `m(a)`, or `m(a, b, c: c)` instead of `m(a, b, c)`.. -- https://bugs.ruby-lang.org/ Unsubscribe: