ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nardonykolyszyn@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90920] [Ruby trunk Bug#15515] OpenStruct raising NoMethodError instead of ArgumentError
Date: Mon, 07 Jan 2019 16:21:45 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-76112.20190107162144.0c8240c08dea4d88@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15515.20190107134140@ruby-lang.org

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/

  parent reply	other threads:[~2019-01-07 16:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15515.20190107134140@ruby-lang.org>
2019-01-07 13:41 ` [ruby-core:90918] [Ruby trunk Bug#15515] OpenStruct raising NoMethodError instead of ArgumentError lisa.ugray
2019-01-07 16:14 ` [ruby-core:90919] " nardonykolyszyn
2019-01-07 16:21 ` nardonykolyszyn [this message]
2019-01-07 16:26 ` [ruby-core:90921] " lisa.ugray
2019-04-12 22:42 ` [ruby-core:92266] " ruby-core
2019-04-14 13:50 ` [ruby-core:92282] " 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-76112.20190107162144.0c8240c08dea4d88@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).