literal quote in an array?

Discussion in 'Ruby' started by Simon Schuster, Sep 24, 2007.

  1. 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.""

    thanks!
     
    Simon Schuster, Sep 24, 2007
    #1
    1. Advertising

  2. Simon Schuster

    Tim Hunter Guest

    Simon Schuster 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.""
    >
    > thanks!


    '"hello"'
    %q{"hello"}
    "\"hello\""
    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Hunter, Sep 24, 2007
    #2
    1. Advertising

  3. This can work if you do not have strange characters in the content:
    sentence = speaker[0] + expression[1] + content[0].inspect

    This is better:
    sentence = speaker[0] + expression[1] + "\"#{content[0]}\""

    2007/9/24, Simon Schuster <>:
    > 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.""
    >
    > thanks!
    >
    >
    > !DSPAM:46f7f33b123781222944467!
    >
    >
     
    Gaspard Bucher, Sep 24, 2007
    #3
  4. Simon Schuster wrote:
    > 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.""


    I'd do it like this:
    speaker = %w(joe betty)
    expression = %w(said replied stated)
    content = %w(hello. bye.)
    sentence = "#{speaker[0]} #{expression[1]}, #{content[0].inspect}"

    If using inspect feels too hackish to you, you could use \"#{content[0]}\"
    instead.


    HTH,
    Sebastian
    --
    NP: Shape of Despair - Angels of Distress
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 24, 2007
    #4
  5. On Sep 24, 2007, at 1:37 PM, Gaspard Bucher wrote:
    > 2007/9/24, Simon Schuster <>:
    >> 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.""
    >>
    >> thanks!
    >>
    >> !DSPAM:46f7f33b123781222944467!

    > This can work if you do not have strange characters in the content:
    > sentence = speaker[0] + expression[1] + content[0].inspect
    >
    > This is better:
    > sentence = speaker[0] + expression[1] + "\"#{content[0]}\""


    Or better still:

    sentence = %{#{speaker[0]} #{expression[1]} "#{content[0]}"}

    Then you don't need the extra spaces in the speaker and expression
    elements.

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Sep 24, 2007
    #5
  6. excellent, thank you

    On 9/24/07, Gaspard Bucher <> wrote:
    > This can work if you do not have strange characters in the content:
    > sentence = speaker[0] + expression[1] + content[0].inspect
    >
    > This is better:
    > sentence = speaker[0] + expression[1] + "\"#{content[0]}\""
    >
    > 2007/9/24, Simon Schuster <>:
    > > 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.""
    > >
    > > thanks!
    > >
    > >
    > > !DSPAM:46f7f33b123781222944467!
    > >
    > >

    >
    >
     
    Simon Schuster, Sep 24, 2007
    #6
  7. Simon Schuster

    Phrogz Guest

    On Sep 24, 11:28 am, "Simon Schuster" <> 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
     
    Phrogz, Sep 24, 2007
    #7
  8. Simon Schuster

    Phrogz Guest

    On Sep 24, 12:38 pm, Phrogz <> wrote:
    > 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


    Oops, I forgot that this relies on having:

    class Array
    def random
    self[ rand( self.length ) ]
    end
    end
     
    Phrogz, Sep 24, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    13,324
  2. Stimp
    Replies:
    2
    Views:
    2,291
    Stimp
    Sep 20, 2006
  3. Eric Layman
    Replies:
    3
    Views:
    644
    Rad [Visual C# MVP]
    Apr 14, 2007
  4. Replies:
    6
    Views:
    447
    Stefan Ram
    Jun 15, 2008
  5. Valery Khamenya
    Replies:
    3
    Views:
    728
Loading...

Share This Page