ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:47515] Hash#to_hashがHashを返さない
@ 2013-07-16 23:26 xibbar
  2013-07-17  1:38 ` [ruby-dev:47516] Hash#to_hashがHashを返さない Akinori MUSHA
  0 siblings, 1 reply; 2+ messages in thread
From: xibbar @ 2013-07-16 23:26 UTC (permalink / raw
  To: ruby developers list

xibbarこと藤岡です。

表題はちょっぴりあおり気味ですが、
RubyのHash#to_hashはHashを返さずにselfを返します。
Hashのまま使っているんだったらいいのですが、
これを継承すると、
% irb
irb(main):001:0> class MyHash<Hash;end
=> nil
irb(main):002:0> myhash=MyHash.new
=> {}
irb(main):003:0> myhash.to_hash
=> {}
irb(main):004:0> myhash.to_hash.class
=> MyHash
となってしまいます。
これ、Array#to_aやString#to_sだと
ArrayとStringをそれぞれ返します。

Hash#to_hashはHashを返したほうがいいのではないでしょうか?
現状、継承したHashからHashを得るためには
Hash[myhash]とやるのが正解のようで、
これはこれで裏技に近いと思いました。

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-dev:47516] Re: Hash#to_hashがHashを返さない
  2013-07-16 23:26 [ruby-dev:47515] Hash#to_hashがHashを返さない xibbar
@ 2013-07-17  1:38 ` Akinori MUSHA
  0 siblings, 0 replies; 2+ messages in thread
From: Akinori MUSHA @ 2013-07-17  1:38 UTC (permalink / raw
  To: ruby developers list, xibbar; +Cc: ruby developers list

[-- Attachment #1: Type: text/plain, Size: 1929 bytes --]

At Wed, 17 Jul 2013 08:26:03 +0900,
xibbar wrote:
> 表題はちょっぴりあおり気味ですが、
> RubyのHash#to_hashはHashを返さずにselfを返します。
> Hashのまま使っているんだったらいいのですが、
> これを継承すると、
> % irb
> irb(main):001:0> class MyHash<Hash;end
> => nil
> irb(main):002:0> myhash=MyHash.new
> => {}
> irb(main):003:0> myhash.to_hash
> => {}
> irb(main):004:0> myhash.to_hash.class
> => MyHash
> となってしまいます。
> これ、Array#to_aやString#to_sだと
> ArrayとStringをそれぞれ返します。
>
> Hash#to_hashはHashを返したほうがいいのではないでしょうか?
> 現状、継承したHashからHashを得るためには
> Hash[myhash]とやるのが正解のようで、
> これはこれで裏技に近いと思いました。

Array#to_a に相当するのは Hash#to_h です。こいつは継承してもHashを返し
ますよ。myhash.to_h でOKのはず。

to_str/to_ary/to_hash とフルネームの変換メソッドは、本当にそれぞれ
String/Array/Hash に暗黙の変換もしてほしいというとき「だけ」、それぞれ
のクラスのインスタンスを返すように(再)定義すべしというのが決まりです。
(Rubyでは Klass.try_convert, C APIでは rb_convert_type()等で暗黙に使わ
れるため)

従って、必要なら継承した上で to_hash を再定義すべき(単純なケースなら
alias to_hash to_h)というのが模範解答だと思います。

ただ、 Array#to_a/to_ary はそうなっているけど String#to_s/to_str はそう
ではないようです。おそらく、 String を継承するのは String の拡張した何
かを作りたいケースなので String への自動変換は望まれることが自然だが、
Array/Hash はコンテナであり、拡張というよりは内包するより楽なので継承す
るというケースが多いので自動変換は必ずしも望まれないということなのかな
というのが私の推測です。

--
Akinori MUSHA / http://akinori.org/

[-- Attachment #2: Type: application/pgp-signature, Size: 196 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-07-17  2:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-16 23:26 [ruby-dev:47515] Hash#to_hashがHashを返さない xibbar
2013-07-17  1:38 ` [ruby-dev:47516] Hash#to_hashがHashを返さない Akinori MUSHA

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).