From: "Jesús Gabriel y Galán" Date: 2012-08-17T15:19:15+09:00 Subject: Re: variable from array to function to array On Fri, Aug 17, 2012 at 2:39 AM, Dave Castellano wrote: > anyone know if it is possible to pass elements from an array (eg 20,50 ) > to the function > number_range(min,max) and have the returned value used in the variable > |age| below... > > > def number_range(min,max) # Chooses a random number within > specified range. > num = min + rand(max - min) > end > > array = [ lambda { |age| "An #{age} year old..."}, 20, 50 ] > > puts array[0][age returned from number_range method] > > >>> An 18 year old... The obvious thing would be: puts array[0][number_range(array[1], array[2])] but this data structure starts to be too complicated. Why is it an array? Will you have some (many) more entries (lambda + range)? If so, you could be better off with a class to represent that information: RangedInterpolation = Struct.new :lambda, :min, :max array = [ RangedInterpolation.new(lambda { |age| "An #{age} year old..."}, 20, 50) ] array.each {|interpolation| puts interpolation.lambda(number_range(interpolation.min, interpolation.max))} You could go one step further and make the pair min,max a Range object, and even include the number_range method inside the class. Jesus.