Issue #18573 has been updated by jeremyevans0 (Jeremy Evans).
I developed a patch for this that implemented the feature using `Array.pack1` and was
going to create a new feature request for it, but I'm glad to see there already is an
existing feature request for it. Here's my pull request for it:
https://github.com/ruby/ruby/pull/8598
----------------------------------------
Feature #18573: Object#pack1
https://bugs.ruby-lang.org/issues/18573#change-104824
* 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/