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=-4.0 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,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 C30921F4C0 for ; Fri, 25 Oct 2019 19:32:25 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id C2C40120BAA; Sat, 26 Oct 2019 04:32:14 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 2FD4D120B8B for ; Sat, 26 Oct 2019 04:32:08 +0900 (JST) Received: by filter0098p3las1.sendgrid.net with SMTP id filter0098p3las1-21024-5DB34DBC-E 2019-10-25 19:32:12.205420229 +0000 UTC m=+336234.347225075 Received: from herokuapp.com (unknown [18.208.173.77]) by ismtpd0003p1iad1.sendgrid.net (SG) with ESMTP id n_HJ5G1ISbavNHypxz4Ugg for ; Fri, 25 Oct 2019 19:32:12.081 +0000 (UTC) Date: Fri, 25 Oct 2019 19:32:12 +0000 (UTC) From: ruby-core@marc-andre.ca Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71142 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15815 X-Redmine-Issue-Author: mtsmfm X-Redmine-Issue-Assignee: marcandre X-Redmine-Sender: marcandre 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?jp+swDN4yjawvlS938eDTV7AcuBgTjW2xvQn0nZL4RDG1ib=2FLPiyu14s=2FJ1EVF?= =?us-ascii?Q?9Vzaq4UZZ62dmlcaV5a23QY4+vl2lBf2at5XSPa?= =?us-ascii?Q?PuutCU0t8ytYN1EZF4KA7PNNDo4mxRBQxKXTb4w?= =?us-ascii?Q?=2FpIxv0TNLK08Nlv4pOKBWwMt64jAjb2t3aGRwMg?= =?us-ascii?Q?iLNrCfTmFEyzLuLWnKvprOcnl=2FgOSvdouPw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95550 Subject: [ruby-core:95550] [Ruby master 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 marcandre (Marc-Andre Lafortune). Assignee set to marcandre (Marc-Andre Lafortune) ---------------------------------------- Feature #15815: Add option to raise NoMethodError for OpenStruct https://bugs.ruby-lang.org/issues/15815#change-82327 * Author: mtsmfm (Fumiaki Matsushima) * Status: Open * Priority: Normal * Assignee: marcandre (Marc-Andre Lafortune) * 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/