LoginSignup
0
0

More than 5 years have passed since last update.

配列要素をClass名で振り分けたHashに

Posted at

仕事中にちょっと書いてみたんですが、実際やっぱりこんなことしなくても良かったので供養。

target = [1, :hoge, "Foo", :fuga, "Bar", 2]
target.inject({}){|acc, obj| (acc[obj.class.to_s.intern]||=[]) << obj; acc}
#=> {:Fixnum=>[1, 2], :Symbol=>[:hoge, :fuga], :String=>["Foo", "Bar"]}
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0