ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: mtsmfm@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:94320] [Ruby master Feature#15815] Add option to raise NoMethodError for OpenStruct
Date: Tue, 13 Aug 2019 09:30:10 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-80707.20190813093010.8b815b31c32404a8@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15815.20190501135220@ruby-lang.org

Issue #15815 has been updated by mtsmfm (Fumiaki Matsushima).


koi-san

Sorry for being late.
I've added https://bugs.ruby-lang.org/issues/15996?next_issue_id=15993&prev_issue_id=15998#note-17

----------------------------------------
Feature #15815: Add option to raise NoMethodError for OpenStruct
https://bugs.ruby-lang.org/issues/15815#change-80707

* 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/

  parent reply	other threads:[~2019-08-13  9:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15815.20190501135220@ruby-lang.org>
2019-05-01 13:52 ` [ruby-core:92513] [Ruby trunk Feature#15815] Add option to raise NoMethodError for OpenStruct mtsmfm
2019-05-01 17:09 ` [ruby-core:92518] " ruby-core
2019-05-01 17:47 ` [ruby-core:92519] " shevegen
2019-07-29  6:37 ` [ruby-core:93965] [Ruby master " ko1
2019-08-13  9:30 ` mtsmfm [this message]
2019-08-13 10:15 ` [ruby-core:94321] " manga.osyo
2019-08-23 22:41 ` [ruby-core:94514] " esparta
2019-08-29  7:09 ` [ruby-core:94656] " matz
2019-10-25 19:32 ` [ruby-core:95550] " ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-80707.20190813093010.8b815b31c32404a8@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).