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=-2.6 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_HELO_NONE,SPF_PASS shortcircuit=no autolearn=no 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 F22B01F461 for ; Thu, 22 Aug 2019 12:01:40 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 59856120AD1; Thu, 22 Aug 2019 21:01:33 +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 CC0FE120AC3 for ; Thu, 22 Aug 2019 21:01:30 +0900 (JST) Received: by filter0005p3iad2.sendgrid.net with SMTP id filter0005p3iad2-7372-5D5E841D-52 2019-08-22 12:01:33.725340712 +0000 UTC m=+559078.530716165 Received: from herokuapp.com (unknown [54.226.103.57]) by ismtpd0046p1mdw1.sendgrid.net (SG) with ESMTP id lt0crTHjSfO2Ti5CRsZVQw for ; Thu, 22 Aug 2019 12:01:33.670 +0000 (UTC) Date: Thu, 22 Aug 2019 12:01:33 +0000 (UTC) From: kimmo.lehto@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70029 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16115 X-Redmine-Issue-Author: kke X-Redmine-Sender: kke 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: =?us-ascii?Q?uzb5OtJTcS8YkZPFqpyVChJjadJHV+GHsFHgSjmJp9JG4EofT5WP=2FOdm4VDtlk?= =?us-ascii?Q?xecm4ZIdTGNDFK0gJ7EfJCoTXQ4jImR7cKAetNO?= =?us-ascii?Q?6dbqXkrqj+zYC5UvNqDE0Pf++36tmCk0pB2UZVx?= =?us-ascii?Q?tCn5pU6UhboUHX9MVw82sloUgsUWacQ5pscve9n?= =?us-ascii?Q?uDZphP32ZrU=2F=2FG8dmrpqhLiRlfuKdj1ncBw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94480 Subject: [ruby-core:94480] [Ruby master Feature#16115] Keyword arguments from method calls or ignore extra hash keys in splat 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 #16115 has been reported by kke (Kimmo Lehto). ---------------------------------------- Feature #16115: Keyword arguments from method calls or ignore extra hash keys in splat https://bugs.ruby-lang.org/issues/16115 * Author: kke (Kimmo Lehto) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Just a thought, feel free to insta-close as stupid. Currently you can do this: ``` def hello(who:) puts "Hello, #{who}" end opts = { who: 'world' } hello(who) ``` Or: ``` def hello(who:, **_extra_opts) # without eating extra args, you get ArgumentError (unknown keyword: foo) puts "Hello, #{who}!" end hello(**{ who: 'world', foo: 'bar' }) ``` Or even this: ``` def hello(_obj = nil, who: _obj&.who || raise(ArgumentError, "missing who:")) puts "Hello, #{who}!" end hello(OpenStruct.new(who: 'world')) ``` 1) What if when you passed an object as an argument to a method that only accepts keyword arguments, the methods listed would be called? Then you could do: ``` require 'ostruct' ostruct = OpenStruct.new(who: 'world, foo: 'bar') def hello(who:) puts "Hello, #{who}!" end hello(ostruct) ``` 2) Or perhaps add some sort of "triple splat" that would either a) do the method calling thing described above or b) do the regular hash splatting like `**` but ignore any keys present that are not listed as keyword arguments: ``` require 'ostruct' ostruct = OpenStruct.new(who: 'world', foo: 'bar') def hello(who:) puts "Hello, #{who}!" end # a: hello(***ostruct) # would call ostruct.who to figure out `who:` # or b: (currently with ** raises ArgumentError (unknown keyword: foo)) hello(***{ who: 'world', foo: 'bar' }) # would ignore any extra keys ``` I think this could open up some possibilities (or a can of worms). -- https://bugs.ruby-lang.org/