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.9 required=3.0 tests=AWL,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 07A271F453 for ; Wed, 1 May 2019 17:48:04 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id A7411120AC8; Thu, 2 May 2019 02:47:58 +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 031B2120AAE for ; Thu, 2 May 2019 02:47:55 +0900 (JST) Received: by filter0159p3mdw1.sendgrid.net with SMTP id filter0159p3mdw1-11212-5CC9DBCC-22 2019-05-01 17:47:56.8615753 +0000 UTC m=+509252.173009392 Received: from herokuapp.com (unknown [54.159.201.37]) by ismtpd0040p1iad2.sendgrid.net (SG) with ESMTP id gqH-RWCRQmeWW9ZKMkaPNw for ; Wed, 01 May 2019 17:47:56.639 +0000 (UTC) Date: Wed, 01 May 2019 17:47:57 +0000 (UTC) From: shevegen@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 68002 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15815 X-Redmine-Issue-Author: mtsmfm X-Redmine-Sender: shevegen 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?6lbdtOg4RDRLuxD00eQtQKgoNAsge5d4xND7cbMQd0xmlhmDTejhEMuXSTfSvl?= =?us-ascii?Q?pV7pIxgjSa1WG07vlPwOJgnx715WeoWgHYO2nwM?= =?us-ascii?Q?JWupV6elRXIwtIhioKvQS9GyFJuXb+Phlc=2Fz0bp?= =?us-ascii?Q?8AkUD+cQmWCo56BH86wN1HLr3yTmpO7LN6aXg+P?= =?us-ascii?Q?Mr7+HuUSR3qLG2e179cykcuX8YtaJ0=2FsM=2Fg=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 92519 Subject: [ruby-core:92519] [Ruby trunk Feature#15815] Add option to raise NoMethodError for OpenStruct 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 #15815 has been updated by shevegen (Robert A. Heiler). > Note that you might be able to write users.map{@[:id]} in the next version and > avoid using an OpenStruct altogether. I think that the idiom, or "mental mode", e. g. how users use ruby, is a bit different between the two examples. At the least to me, "OpenStruct" is instantly recognizable and "exception: true" is also no longer a rare idiom, after some core methods accepted it; the map {@[:id] variant, at the least to me, is slightly harder to understand for my brain. But anyway, that's just my opinion - to the suggestion itself, one described use case: > But I want to prevent typo for a key name. Although I personally do not use struct and openstruct a lot, even though I think it is a cool idea (prototypical objects all the way, at all times), to me the suggestion appears useful. So I am in light support of the suggestion; I don't feel particularly strong either way, though. (May help for people to comment who actually use struct and openstruct a lot.) ---------------------------------------- Feature #15815: Add option to raise NoMethodError for OpenStruct https://bugs.ruby-lang.org/issues/15815#change-77883 * Author: mtsmfm (Fumiaki Matsushima) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- GitHub PR: https://github.com/ruby/ruby/pull/2164 Currently, `OpenStruct#method_missing` returns `nil` even if the key isn't registered. ```ruby require 'ostruct' os = OpenStruct.new({a: 1}) os.a #=> 1 os.b #=> nil ``` I'd like to add `exception` option to raise `NoMethodError` in such case. ```ruby require 'ostruct' os = OpenStruct.new({a: 1}, exception: true) os.a #=> 1 os.b #=> NoMethodError ``` ## Use case I sometimes use OpenStruct as a JSON API response wrapper. It's useful to use method call instead of key access (`obj[:key]`) because we can use `Symbol#to_proc` if it's a method (for example `users.map(&:id)`) But I want to prevent typo for a key name. Currently `users.map(&:idd)` just returns `[nil,...]` Even if we have this `exception` option, we can't enable this option for JSON parser easily though: ```ruby JSON.parse(response, object_class: Class.new(OpenStruct) { def initialize(hash); super(hash, exception: true); end }) ``` What do you think? ---- I've searched with "openstruct nomethoderror" on bugs.ruby-lang.org though, please let me know if it's duplicated. https://bugs.ruby-lang.org/search?utf8=%E2%9C%93&scope=&q=nomethoderror+openstruct -- https://bugs.ruby-lang.org/