From: sto.mar@... Date: 2012-08-18T14:13:48+09:00 Subject: Re: variable from array to function to array Here an improved version that allows you to use an arbitrary, possible different set of properties for each question. Random values are returned if you provide a Range or an Array. (And it now uses the hash interpolation pointed out by Sean instead of erb,) class Question def initialize(text, substitutions) @text = text @substitutions = substitutions end def to_s subst = {} @substitutions.each do |k, v| if v.class == Range subst[k] = rand(v) elsif v.class == Array subst[k] = v.sample else subst[k] = v end end @text % subst end end # You can use arbitrary keys. # For ranges or arrays a random number / element is used. questions = [ Question.new('A slide is placed %{val} %{unit} %{side} of...', {:val => 20..40, :unit => 'cm', :side => ['left', 'right']}), Question.new('An image is ... %{val} mm ... and %{factor} times...', {:val => 10..15, :factor => 2..9}) ] questions.each {|q| puts q } questions.each {|q| puts q } # gives different values --