Issue #15236 has been updated by ignatiusreza (Ignatius Reza Lesmana).


Hi guys,

Thanks for the discussions! Sorry I didn't noticed that it was proposed (multiple times) before.. I tried to search, but couldn't find a hit..

The ES6 syntax that this gets inspired from is strongly becoming the standard now, partly thanks to it being enabled by default in https://www.npmjs.com/package/eslint-config-airbnb-base

I found a strong the desire for this syntax especially when working on API server alongside JavaScript heavy front end, where one would need to work a lot with building hashes to be transformed into JSON string.. hence, the primary use case where i'm interested in is in building hashes as return value of method call, e.g.

~~~ ruby
def respond_with(resource, options)
  meta = extract_meta(resource, options)
  etc = extract_etc(resource, options)

  { resource, meta, etc }
end
~~~

having

~~~ ruby
{ resource, meta, etc }
~~~

is much more concise and cleaner compared to

~~~ ruby
{ resource: resource, meta: meta, etc: etc }
~~~

within this context, `{ }` is already non-optional, and the new syntax increase readability and save a lot of typing..

To address the concern in https://bugs.ruby-lang.org/issues/11105 .. I think, I agree that this shorthand syntax should only be allowed for `a`, but not for `@a`, `@@a`, or `$a` to avoid ambiguity in what key should be generated for everything else other than `a`..

----------------------------------------
Feature #15236: add support for hash shorthand
https://bugs.ruby-lang.org/issues/15236#change-74556

* 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: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>