[#99868] [Ruby master Bug#17144] Tempfile.open { ... } does not unlink the file — eregontp@...
Issue #17144 has been reported by Eregon (Benoit Daloze).
15 messages
2020/09/03
[ruby-core:100191] [Ruby master Feature#15815] Add option to raise NoMethodError for OpenStruct
From:
mtsmfm@...
Date:
2020-09-28 02:11:34 UTC
List:
ruby-core #100191
Issue #15815 has been updated by mtsmfm (Fumiaki Matsushima).
Cool.
It's much better API to integrate with `JSON.parse`.
----------------------------------------
Feature #15815: Add option to raise NoMethodError for OpenStruct
https://bugs.ruby-lang.org/issues/15815#change-87763
* Author: mtsmfm (Fumiaki Matsushima)
* Status: Open
* Priority: Normal
* Assignee: marcandre (Marc-Andre Lafortune)
----------------------------------------
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/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>