On Sep 24, 11:28 am, "Simon Schuster" <significa... / gmail.com> wrote:
> using colons for now, but would like to somehow encapsulate a sentence
> in this way (basically), with quotes.
>
> speaker = ["joe ", "betty "]
> expression = ["said: ", "replied: ", "stated: "]
> content = ["hello.", "bye."]
>
> 053:0> sentence = speaker[0] + expression[1] + content[0]
> "joe replied: hello."
> but I'd like to get:
> "joe replied, "hello.""

Seeing what you're doing, I thought I'd show you my "String#variation"
method, that allows you to write a generic sentence like this:
  q = "(How (much|many)|What) is (the (value|result) of)? "
and generate random variations on it via:
  q.variation

If you have variables you want to substitute into the string, you can
either do that during construction (if they never change):
  q = "(Hello|Howdy), #{name}"
or you can substitute the name variable on the fly:
  q = "(Hello|Howdy), :name"
  greeting = q.variation( :name=>"Bob" )

I wrote this code for my solution to Quiz #48 [1], and have only ever
used it there, but it's reasonably simple and seems solid enough.

class String
   def variation( values={} )
     out = self.dup
     while out.gsub!( /\(([^())?]+)\)(\?)?/ ){
       ( $2 && ( rand > 0.5 ) ) ? '' : $1.split( '|' ).random
     }; end
     out.gsub!( /:(#{values.keys.join('|')})\b/ ){ values[$1.intern] }
     out.gsub!( /\s{2,}/, ' ' )
     out
   end
end


Perhaps you will find it useful.

[1] http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/157538