Mega Code Archive

 
Categories / Ruby / Method
 

Use lambda to create a list of function

def times_n(n)   lambda { |x| x * n } end times_ten = times_n(10) times_ten.call(5)                                    # => 50 times_ten.call(1.25)                                 # => 12.5 circumference = times_n(2*Math::PI) circumference.call(10)                               # => 62.8318530717959 circumference.call(3)                                # => 18.8495559215388 p [1, 2, 3].collect(&circumference)