From: ruby-core@marc-andre.ca
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90494] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method'
Date: Thu, 13 Dec 2018 06:59:43 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-75652.20181213065941.c7d94743e9930d9c@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15409.20181213032526@ruby-lang.org
Issue #15409 has been updated by marcandre (Marc-Andre Lafortune).
I don't have a good solution.
`OpenStruct` is kind of an anti-pattern. This is even more apparent when adding methods to `Object/Kernel`.
I note that `Struct` allows overriding builtin methods:
Struct.new(:method, keyword_init: true).new(method: :foo).method # => :foo
I'd be tempted to allow overriding of methods in `OpenStruct` for setters (e.g. `method=`), but not very confident about it.
Even if we allow overriding like this, this could still yield to potentially unexpected results.
o = OpenStruct.new
o.then # => nil in Ruby 2.5, Enumerator in Ruby 2.6
o.then = :foo # (assuming we allow overriding from setters)
o.then # => :foo
If the object is inited with the data like `OpenStruct.new(then: :foo)`, then both versions would at least behave the same.
----------------------------------------
Bug #15409: OpenStruct error when attribute is called 'method'
https://bugs.ruby-lang.org/issues/15409#change-75652
* Author: elioncho (Elías Orozco)
* Status: Assigned
* Priority: Normal
* Assignee: marcandre (Marc-Andre Lafortune)
* Target version:
* ruby -v: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin16]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN
----------------------------------------
The following error is shown when you try to access an OpenStruct with a property called method:
`method': wrong number of arguments (given 0, expected 1) (ArgumentError)
To replicate:
~~~ ruby
require 'ostruct'
o = OpenStruct.new({ method: 'get' })
o.method
~~~
The expected behavior should be to return 'get'
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2018-12-13 6:59 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-15409.20181213032526@ruby-lang.org>
2018-12-13 3:25 ` [ruby-core:90481] [Ruby trunk Bug#15409] OpenStruct error when attribute is called 'method' elioncho
2018-12-13 5:46 ` [ruby-core:90491] " oleynikov
2018-12-13 6:17 ` [ruby-core:90492] [Ruby trunk Bug#15409][Assigned] " mame
2018-12-13 6:59 ` ruby-core [this message]
2018-12-13 7:23 ` [ruby-core:90497] [Ruby trunk Bug#15409] " shevegen
2018-12-13 8:21 ` [ruby-core:90499] " mame
2019-05-09 15:15 ` [ruby-core:92617] " tansaku
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-75652.20181213065941.c7d94743e9930d9c@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).