Beesbot

Functional Ruby Fu!

May 15, 2013

For some strange reason I keep forgetting ruby folding functions. So for my future self...

Reject

[1, 2, 3, 4, 5].reject {|x| x%2==0}
=> [1, 3, 5]

Select

[1, 2, 3, 4, 5].select {|x| x%2==0}
=> [2, 4]

Collect

[1, 2, 3, 4, 5].collect {|x| x*2}
=> [2, 4, 6, 8, 10]

Inject

sum = 0
[1, 2, 3, 4, 5].inject {|sum, x| sum + x}
=> 15

Now I will never forget!