[ruby-core:94198] [Ruby master Bug#16086] OpenStruct method access with a block does not raise
From:
nobu@...
Date:
2019-08-08 13:22:38 UTC
List:
ruby-core #94198
Issue #16086 has been updated by nobu (Nobuyoshi Nakada).
Status changed from Open to Feedback
kke (Kimmo Lehto) wrote:
> This can cause confusion.
>
> ```ruby
> > OpenStruct.new(hello: 'world').each { |k, v| puts k.upcase } # there's no "each" method
> => nil
> > OpenStruct.new(hello: 'world').eahc_pair { |k, v| puts k.upcase } # each_pair typo
> => nil
> ```
>
> An undefined key with a block should maybe raise `NoMethodError` or `ArgumentError`?
That is `OpenStruct`, undefined key does not raise an exception.
And unused block is silently ignored in common.
> A defined key with a block seems to yield the value, I don't see this being documented:
>
> ```ruby
> > OpenStruct.new(hello: 'world').hello { |k| puts k.upcase }
> HELLO
> ```
>
> Maybe it should raise `ArgumentError` too?
I haven't seen such behavior.
```
$ ruby -rostruct -e 'p OpenStruct.new(hello: "world").hello { |k| puts k.upcase }'
"world"
```
----------------------------------------
Bug #16086: OpenStruct method access with a block does not raise
https://bugs.ruby-lang.org/issues/16086#change-80493
* Author: kke (Kimmo Lehto)
* Status: Feedback
* Priority: Normal
* Assignee:
* Target version:
* ruby -v:
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
This can cause confusion.
```ruby
> OpenStruct.new(hello: 'world').each { |k, v| puts k.upcase } # there's no "each" method
=> nil
> OpenStruct.new(hello: 'world').eahc_pair { |k, v| puts k.upcase } # each_pair typo
=> nil
```
An undefined key with a block should maybe raise `NoMethodError` or `ArgumentError`?
A defined key with a block seems to yield the value, I don't see this being documented:
```ruby
> OpenStruct.new(hello: 'world').hello { |k| puts k.upcase }
HELLO
```
Maybe it should raise `ArgumentError` too?
Related: https://bugs.ruby-lang.org/issues/15515
--
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>