From: "byroot (Jean Boussier)" <noreply@ruby-lang.org>
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:51158] [Ruby master Feature#18573] Object#pack1
Date: Tue, 08 Feb 2022 08:51:37 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-96419.20220208085136.33140@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-18573.20220206163301.33140@ruby-lang.org>
Issue #18573 has been updated by byroot (Jean Boussier).
(using Google Translate to understand the discussion, sorry if I misunderstood or missed things).
Since `Object#pack1` might be deemed a bit too invasive, an alternative could be `String.pack1(format, arg)`, or even `String.pack(format, *args)`.
----------------------------------------
Feature #18573: Object#pack1
https://bugs.ruby-lang.org/issues/18573#change-96419
* Author: os (Shigeki OHARA)
* Status: Open
* Priority: Normal
----------------------------------------
# 概要
String#unpack1 の逆の Object#pack1 が欲しい。
# 背景
Array#pack というメソッドがありますが、レシーバーの Array の要素数が 1 つしかないことが良くあります。
``` ruby
[codepoint].pack('U')
[digest].pack('m0')
[mail_body].pack('M')
[ip_address].pack('N')
```
標準添付ライブラリーなどを眺めてみてもチラホラあるようです。
ですが、このようなケースで変換対象のオブジェクトをわざわざ Array でくるまなくてはいけないというのは面倒な気もします。
# 提案
String#unpack に対して String#unpack1 というメソッドがありますが、
Array#pack に対する Object#pack1 というメソッドを提案します。
イメージとしては以下のコードのような感じです。
``` ruby
class Object
def pack1(template, option = {})
[self].pack(template, **option)
end
end
```
# 議論・課題
* Object で良いかどうかは議論の余地があろうかと思います
* メソッド名が pack1 で良いかはわかりませんが、他とかぶる可能性は低いかと思います
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2022-02-08 8:51 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-06 16:33 [ruby-dev:51154] " os (Shigeki OHARA)
2022-02-07 2:49 ` [ruby-dev:51155] " knu (Akinori MUSHA)
2022-02-07 3:02 ` [ruby-dev:51156] " knu (Akinori MUSHA)
2022-02-07 3:19 ` [ruby-dev:51157] " knu (Akinori MUSHA)
2022-02-08 8:51 ` byroot (Jean Boussier) [this message]
2023-10-05 21:21 ` [ruby-dev:52050] " jeremyevans0 (Jeremy Evans) via ruby-dev
2023-10-06 14:12 ` [ruby-dev:52051] " Eregon (Benoit Daloze) via ruby-dev
2023-10-12 4:21 ` [ruby-dev:52052] " matz (Yukihiro Matsumoto) via ruby-dev
2023-10-12 19:11 ` [ruby-dev:52053] " tenderlovemaking (Aaron Patterson) via ruby-dev
2023-10-13 13:00 ` [ruby-dev:52054] " mame (Yusuke Endoh) via ruby-dev
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-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-96419.20220208085136.33140@ruby-lang.org \
--to=noreply@ruby-lang.org \
--cc=ruby-dev@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 NNTP newsgroup(s).