From: "byroot (Jean Boussier)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:109865] [Ruby master Bug#18998] Kernel#Integer does not convert SimpleDelegator object expectly
Date: Fri, 09 Sep 2022 22:14:30 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-99105.20220909221429.52440@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18998.20220908132131.52440@ruby-lang.org
Issue #18998 has been updated by byroot (Jean Boussier).
> My thought is that Integer method should convert a Delegator object like when a String is given.
I don't think it would be advisable for `Kernel#Integer` to know about `Delegator`. `Delegator` is just a class from the stdlib, you could implement the same yourself and that wouldn't solve your problem.
I think based on the spec lined up above, it would be best if you defined `#to_int` on your delegator. But I don't know your full use case, so not sure if it's relevant.
Either way I don't think there is really a bug here, aside from maybe the method needed more documentation.
A change of behavior would fall under a feature request, and a proper spec would need to be drafted. The only possible change I could see would be to try to call `to_str` between `to_int` and `to_i`, but that could be a breaking change, so a hard sell.
----------------------------------------
Bug #18998: Kernel#Integer does not convert SimpleDelegator object expectly
https://bugs.ruby-lang.org/issues/18998#change-99105
* Author: taichi730 (Taichi Ishitani)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
Kernel#Integer method doens not convert a SimpleDelegator object of which value is a String.
This is an sample code.
``` ruby
require 'delegate'
p Integer(SimpleDelegator.new('0x10'))
```
I expect Kernel#Integer to convert the input value as a String and the expected returned value is `16`.
Hoever the actual returned value is `0` so it's seemed that `#to_i` method is just called.
```
taichi@LAPTOP-TVTKLNFD:temp
$ cat test.rb
require 'delegate'
p Integer(SimpleDelegator.new('0x10'))
taichi@LAPTOP-TVTKLNFD:temp
$ ruby test.rb
0
```
Which is the correct behavior?
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2022-09-09 22:14 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-08 13:21 [ruby-core:109848] [Ruby master Bug#18998] Kernel#Integer does not convert SimpleDelegator expectly taichi730 (Taichi Ishitani)
2022-09-08 19:35 ` [ruby-core:109855] [Ruby master Bug#18998] Kernel#Integer does not convert SimpleDelegator object expectly byroot (Jean Boussier)
2022-09-09 17:10 ` [ruby-core:109862] " taichi730 (Taichi Ishitani)
2022-09-09 22:14 ` byroot (Jean Boussier) [this message]
2022-09-10 12:31 ` [ruby-core:109871] " taichi730 (Taichi Ishitani)
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-99105.20220909221429.52440@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).