Skip to content

Instantly share code, notes, and snippets.

@yancya
Forked from naoty/gist:6914586
Last active December 25, 2015 04:09
Show Gist options
  • Save yancya/6914843 to your computer and use it in GitHub Desktop.
Save yancya/6914843 to your computer and use it in GitHub Desktop.
[1] pry(main)> hoges = 10.times.map{{fuga:10, piyo:20}}
=> [{:fuga=>10, :piyo=>20},
{:fuga=>10, :piyo=>20},
{:fuga=>10, :piyo=>20},
{:fuga=>10, :piyo=>20},
{:fuga=>10, :piyo=>20},
{:fuga=>10, :piyo=>20},
{:fuga=>10, :piyo=>20},
{:fuga=>10, :piyo=>20},
{:fuga=>10, :piyo=>20},
{:fuga=>10, :piyo=>20}]
[2] pry(main)> hoges.map{|h|h[:fuga]}.reduce(&:+)
=> 100
[3] pry(main)> hoges.inject(0){|sum,hoge| sum+=hoge[:fuga]}
=> 100
[4] pry(main)> fuga=Proc.new{|o| o[:fuga]}
=> #<Proc:0x007fd102876e98@(pry):1>
[5] pry(main)> hoges.map(&fuga).reduce(&:+)
=> 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment