Issue #15515 has been updated by devpolish (Nardo Nykolyszyn).


I can't understand why you're trying to pass an argument to a String object, OpenStruct creates attr_reader and attr_accessor based in your keys.

$ foo.methods false #=> [:instance_methods, :instance_methods=, :bar, :bar=]
$foo.bar.class # => String

So, basically you're calling a reader method inside your OpenStruct object which's telling you that doesn't accept any argument, seems totally clear for me.

----------------------------------------
Bug #15515: OpenStruct raising NoMethodError instead of ArgumentError
https://bugs.ruby-lang.org/issues/15515#change-76112

* Author: lugray (Lisa Ugray)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.7.0dev (2019-01-07 trunk 66747) [x86_64-darwin18]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
When an `OpenStruct` has some attribute `foo`, calling the `foo` method with an argument gives a no method error:
```ruby
foo = OpenStuct.new(bar: 'baz')
foo.bar(0) # => NoMethodError (undefined method `bar' for #<OpenStruct bar="baz">)
```

This is confusing, since `foo.respond_to?(:bar) # => true`. I would expect:
```ruby
foo = OpenStuct.new(bar: 'baz')
foo.bar(0) # => ArgumentError (wrong number of arguments (given 1, expected 0))
```

The included fixes this, and adjusts the arity `ArgumentError` for the setter to be in line with the default arity messaging too.

---Files--------------------------------
0001-ostruct.rb-refine-arity-failure-message.patch (1.26 KB)
0001-ostruct.rb-refine-arity-failure-message.patch (1.96 KB)


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