def foo(arg)
return arg
end
def bar(&method)
method.call("WAT")
end
bar(&method(:foo))
# ------------------------------------------------------------------------------------------------------------------------
(1..5).map &->(x){ x*x }
# => [1, 4, 9, 16, 25]
will take each element of the array and compute its power
it is the same as this code:
func = ->(x) { x*x }
(1..5).map &func
for Ruby 1.8:
(1..5).map &lambda {|x| x*x}
# => [1, 4, 9, 16, 25]
To solve your problem you can use Array's method reduce (0 is initial value):
('A'..'K').reduce(0) { |sum,elem| sum + elem.size }
# => 11
Passing a lambda function to reduce is a bit tricky, but the anonymous block is pretty much the same as lambda.
('A'..'K').reduce(0) { |sum, elem| ->(sum){ sum + 1}.call(sum) }
# => 11
Or you could concat letters just like this:
('A'..'K').reduce(:+)
=> "ABCDEFGHIJK"
Convert to lowercase:
('A'..'K').map &->(a){ a.downcase }
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"]