Issue #15815 has been updated by esparta (Espartaco Palma).


Personally I found the proposed signature confusing, more when taking in account that OpenStruct calls:

```ruby
os = OpenStruct.new(a: 1, exception: true)

# vs

os = OpenStruct.new({a: 1}, exception: true)

```

> 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
> ```

How about a subclass of OpenStruct to take this feature?

``` ruby
class ExceptionalOpenStruct < OpenStruct
   def method_missing(mid, *args)
      if args.length == 0
         raise NoMethodError unless @table.key?(mid)
        @table[mid]
      else
         super
      end
   end
end

eostr = ExceptionalOpenStruct.new(a: 1)
eostr.a 
# => 1
eostr.b
# NoMethodError: NoMethodError
ebstr.b = 2
eostr.b
# => 2
```
(I'm probably missing some feature, but that's the idea...)

Another concern is probably the performance: OpenStruct  will check another branch just to verify if now it also need to raise an exception.
The flexibility of OpenStruct makes it not exactly fast, handling exception would make all other uses cases more slow performance wise.[*benchmark needed]

osyo (manga osyo) wrote:
> What about adding block arguments to `OpenStruct.new` like `Hash` instead of options?
> 
> ```ruby
> h = Hash.new {|hash, key|
>   raise(IndexError, "hash[#{key}] has no value")
> }
> 
> # Error: in `block in <main>': hash[1] has no value (IndexError)
> h[1]
> 
> 
> require "ostruct"
> 
> # Adding block arguments like Hash
> os = OpenStruct.new({a: 1}) {|open_struct, method_name|
>   raise(NoMethdError, "undefined method `#{method_name}' for #{open_struct}")
> }
> 
> # Error: in `block in <main>': undefined method `b' for #<OpenStruct a=1> (NoMethodError)
> os.b
> ```



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

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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>