Interpolating a string loaded from a text file

Discussion in 'Ruby' started by John Elrick, Apr 6, 2005.

  1. John Elrick

    John Elrick Guest

    Hoping you guys can help out here. Is there any way to interpolate a
    string that is loaded dynamically?

    I know that in Ruby I can do this:

    my_message = 'Hello World'
    my_template = "The message is #my_message"
    puts my_template <== The message is Hello World

    But what I want to do is

    my_message = 'Hello World'
    my_template =
    File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
    puts my_template <== The message is Hello World


    Anyone have any ideas? Or am I missing something blatantly obvious?

    TIA

    John Elrick
     
    John Elrick, Apr 6, 2005
    #1
    1. Advertising

  2. "John Elrick" <> schrieb im Newsbeitrag
    news:...
    > Hoping you guys can help out here. Is there any way to interpolate a
    > string that is loaded dynamically?
    >
    > I know that in Ruby I can do this:
    >
    > my_message = 'Hello World'
    > my_template = "The message is #my_message"
    > puts my_template <== The message is Hello World
    >
    > But what I want to do is
    >
    > my_message = 'Hello World'
    > my_template =
    > File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
    > puts my_template <== The message is Hello World
    >
    >
    > Anyone have any ideas? Or am I missing something blatantly obvious?


    How about:

    eval "%Q{" + File.read('my_template_file.txt') + "}"

    Kind regards

    robert
     
    Robert Klemme, Apr 6, 2005
    #2
    1. Advertising

  3. On Apr 6, 2005, at 10:34 AM, John Elrick wrote:

    > Hoping you guys can help out here. Is there any way to interpolate a
    > string that is loaded dynamically?
    >
    > I know that in Ruby I can do this:
    >
    > my_message = 'Hello World'
    > my_template = "The message is #my_message"
    > puts my_template <== The message is Hello World


    Only if you replace #my_message with #{my_message}. ;)

    > But what I want to do is
    >
    > my_message = 'Hello World'
    > my_template =
    > File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
    > puts my_template <== The message is Hello World
    >
    >
    > Anyone have any ideas? Or am I missing something blatantly obvious?


    Well, there's always search and replace:

    template.gsub(/#\{\s*(\w+)\s*\}/) { # do something with $1 here... }

    I think that works pretty well with a Hash, keyed by the $1 values to
    interpolate.

    Option two is to call eval(). This executes Ruby code and returns the
    results. This naturally has security concerns, if you aren't in
    control of the template.

    Finally, you can make use of Ruby's standard template library, ERb:

    http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html

    Hope that helps.

    James Edward Gray II
     
    James Edward Gray II, Apr 6, 2005
    #3
  4. John Elrick

    Vraj Mohan Guest

    Will using Kernel#sprintf work achieve the same thing?
    --Vraj Mohan
     
    Vraj Mohan, Apr 6, 2005
    #4
  5. "Vraj Mohan" <> schrieb im Newsbeitrag
    news:...
    > Will using Kernel#sprintf work achieve the same thing?
    > --Vraj Mohan
    >


    No.

    robert
     
    Robert Klemme, Apr 6, 2005
    #5
  6. ke, 2005-04-06 kello 18:49, Robert Klemme kirjoitti:
    > "John Elrick" <> schrieb im Newsbeitrag
    > news:...
    > > my_message = 'Hello World'
    > > my_template =
    > > File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
    > > puts my_template <== The message is Hello World
    > >
    > >
    > > Anyone have any ideas? Or am I missing something blatantly obvious?

    >
    > How about:
    >
    > eval "%Q{" + File.read('my_template_file.txt') + "}"
    >


    echo "}; 10.times{ puts 'do not trust external data'" > \
    my_template_file.txt

    Cheers :)
     
    Ilmari Heikkinen, Apr 6, 2005
    #6
  7. "Ilmari Heikkinen" <> schrieb im Newsbeitrag
    news:1112805304.14517.5.camel@jugend...
    > ke, 2005-04-06 kello 18:49, Robert Klemme kirjoitti:
    > > "John Elrick" <> schrieb im Newsbeitrag
    > > news:...
    > > > my_message = 'Hello World'
    > > > my_template =
    > > >

    File.readlines('my_template_file.txt').HOW-CAN-I-FORCE-INTERPOLATION???
    > > > puts my_template <== The message is Hello World
    > > >
    > > >
    > > > Anyone have any ideas? Or am I missing something blatantly obvious?

    > >
    > > How about:
    > >
    > > eval "%Q{" + File.read('my_template_file.txt') + "}"
    > >

    >
    > echo "}; 10.times{ puts 'do not trust external data'" > \
    > my_template_file.txt
    >
    > Cheers :)


    Hey, you just spoiled my evil - err - eval plan to take over his system!
    :)

    robert
     
    Robert Klemme, Apr 6, 2005
    #7
  8. John Elrick

    Saynatkari Guest

    Le 6/4/2005, "Robert Klemme" <> a écrit:
    >"Vraj Mohan" <> schrieb im Newsbeitrag
    >news:...
    >> Will using Kernel#sprintf work achieve the same thing?
    >> --Vraj Mohan
    >>

    >
    >No.


    Problem is that interpolation is done at the time the String
    is first encountered, which may complicate things since the
    variable used for substitution may not be available, if I am
    understanding correctly.

    However, this works in those in situations:

    # file-one
    str = "Inserted %s."

    # file-two
    require 'file-one'
    puts str % 'this from file two'

    > robert


    E

    No-one expects the Solaris POSIX implementation!
     
    Saynatkari, Apr 6, 2005
    #8
  9. John Elrick

    John Elrick Guest

    Thanks to all who have replied. I appreciate all the solutions.

    Best,

    John
     
    John Elrick, Apr 7, 2005
    #9
  10. John Elrick

    John Carter Guest

    On Thu, 7 Apr 2005, John Elrick wrote:

    > Hoping you guys can help out here. Is there any way to interpolate a
    > string that is loaded dynamically?


    This is my print help routine, the help file itself is almost book length,
    and contains a bunch of #{expressions} that retrieve information from the
    "deliverable_registry" object. (Basically the deliverable_registry
    object knows what can be done, what the command line options to select
    those actions, the descriptions of those actions etc...)

    # Doesn't return.
    # If STDOUT is a terminal pipe the help file through "less".
    def print_help_and_exit(deliverable_registry, plaint = "")


    # Pipe the output through less, unless we are within emacs...
    outf = STDOUT
    if STDOUT.stat.chardev?() && ENV['TERM'] != 'dumb' then
    outf = open( "|less", "w")
    outf = STDOUT if !outf
    end

    eval("
    begin
    outf.puts <<EOHELP
    "+
    File.read( "#{WORK_DIR}/Utilities/inc/build_help.txt")+
    "
    EOHELP # Catch an exit from less
    rescue Errno::EPIPE => details
    $log_file.print_to_screen = 0 # Don't gibber to the screen about the
    # early exit from less
    end
    ")

    outf.close if outf != STDOUT

    # Write the exit reason to the log file..
    $log_file.history plaint
    exit(1)

    # Catch early exit from less
    rescue Errno::EPIPE => details
    $log_file.print_to_screen = 0
    end




    John Carter Phone : (64)(3) 358 6639
    Tait Electronics Fax : (64)(3) 359 4632
    PO Box 1645 Christchurch Email :
    New Zealand

    Refactorers do it a little better every time.
     
    John Carter, Apr 7, 2005
    #10
    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. jitasi
    Replies:
    1
    Views:
    753
    Terry Reedy
    Mar 4, 2007
  2. Lloyd Zusman
    Replies:
    4
    Views:
    210
    Ken Bloom
    Mar 18, 2009
  3. Brian Troutwine

    Lagrange Interpolating Polynomial

    Brian Troutwine, Oct 12, 2004, in forum: Perl Misc
    Replies:
    16
    Views:
    293
    Ilya Zakharevich
    Oct 14, 2004
  4. Replies:
    4
    Views:
    119
  5. Replies:
    3
    Views:
    142
Loading...

Share This Page