From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.1 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 63880211B4 for ; Sat, 12 Jan 2019 15:05:09 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id E94B71219CD; Sun, 13 Jan 2019 00:05:05 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 2B21F1218CF for ; Sun, 13 Jan 2019 00:05:01 +0900 (JST) Received: by filter0040p3iad2.sendgrid.net with SMTP id filter0040p3iad2-22298-5C3A021B-4F 2019-01-12 15:04:59.808044454 +0000 UTC m=+397493.596182226 Received: from herokuapp.com (ec2-54-234-254-147.compute-1.amazonaws.com [54.234.254.147]) by ismtpd0028p1iad2.sendgrid.net (SG) with ESMTP id l7h5wCUPRkWpTg3fsVAzgQ for ; Sat, 12 Jan 2019 15:04:59.711 +0000 (UTC) Date: Sat, 12 Jan 2019 15:05:00 +0000 (UTC) From: janfri26@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 66481 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15526 X-Redmine-Issue-Author: alissonbruno.sa X-Redmine-Sender: janfri X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS57jbfkoqIL+twVUwk12FzT8eQaZ/IpPz8MRO PQ/Qdx3YNBQvP4sutNnqbcdspmZgWSLi8wHA/qn4yRHwyt5dAxpV/bLUOiKqVDZc4MUirkDtfoEIe4 jYJRdHt4nw+rciPN7+Jf6E81K03rhjq+GbD5Uc3R/cq4o+CHMbkc0L+GaQ== X-ML-Name: ruby-core X-Mail-Count: 91047 Subject: [ruby-core:91047] [Ruby trunk Feature#15526] New way to destruct an object hash X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #15526 has been updated by janfri (Jan Friedrich). zverok (Victor Shepelev) wrote: > 1) Ruby has very consistent "argument **deconstruction**" rules. Basically, "it is deconstructed the same way it is constructed", e.g., if you can _call_ (construct) with arguments like `meth(a, *b, c: 1, d: 2, **e)`, you can also _receive_ (deconstruct) arguments exactly the same way: `a, *b, c:, d: nil, **e`. > > 2) The deconstruction is fully enabled in arguments (including keyword arguments) to procs and methods > > 3) Initially (before keyword arguments), the variable assignment had the same "expressive power", e.g. you could define a method like > > ```ruby > def meth(a, b, *rest) > ``` > ...and you could assign variables the same way: > > ```ruby > a, b, *rest = [1, 2, 3, 4] > ``` > > 4) When keyword arguments were introduced, there was no matching syntax for variable assignment. If it **would** exist, it most probably **should** look this way (same as arguments, remember): > > ```ruby > a:, b:, **kwrest = {a: 1, b: 2, c: 3, d: 4} > ``` > It obviously looks confusing (and would probably be hard for the parser), and I believe that's the reason it was NOT added to the language. > > 5) I don't think adding "orphan feature" (looking like nothing else in the language) just for the sake of this particular case would be ever accepted; and I don't think it should. > > 6) In the meantime, "chainable" code style (with `Enumerable` and `#then`) allows to embrace argument deconstruction in its full force: > ```ruby > config.then { |host:, port: 8080, **rest| > ``` > ...which is NOT the main (and obviously not the only) reason to use this style, just a nice side-effect of its consistency with the rest of the language. +1 Great analysis. ---------------------------------------- Feature #15526: New way to destruct an object hash https://bugs.ruby-lang.org/issues/15526#change-76267 * Author: alissonbruno.sa (Alisson Santos) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- JavaScript has a nice a neat way to destruct objects. ~~~ javascript const person = { name: "John Doe", age: 33 }; const { name, age } = person; ~~~ Erlang has a similar way to destruct a tuple: ~~~ erlang Person = {"John Doe", 33} {Name, Age} = Person ~~~ I think it's very handy and would be nice if we have something similar in Ruby. ~~~ ruby config = { host: 'localhost', port: 3000 } { host, port } = config ~~~ I know Ruby has Hash#values_at, but I think this way it's more readable and understandable What do you guys think? -- https://bugs.ruby-lang.org/