Delayed string interpolation

Discussion in 'Ruby' started by Hal Fulton, Feb 8, 2005.

  1. Hal Fulton

    Hal Fulton Guest

    I just wanted to make sure people don't overlook the
    obvious:


    def interp(x,y)
    "The sum of #{x} and #{y} is #{x+y}"
    end

    str = interp(210,27) # "The sum of 210 and 27 is 237"


    Perhaps there are cases where this is no good though.


    Hal
    Hal Fulton, Feb 8, 2005
    #1
    1. Advertising

  2. Hal Fulton <> wrote:
    > def interp(x,y)
    > "The sum of #{x} and #{y} is #{x+y}"
    > end
    >
    > str = interp(210,27) # "The sum of 210 and 27 is 237"


    Unless I'm mistaken, this recomputes the whole string every time
    interp is called. I wanted to compute the common parts of the string,
    cache it somewhere, and then compute the rest in another context.

    Hence, a 2-step interpolation.

    Cheers,
    Navin.
    Navindra Umanee, Feb 8, 2005
    #2
    1. Advertising

  3. Hal Fulton

    Hal Fulton Guest

    Navindra Umanee wrote:
    > Hal Fulton <> wrote:
    >
    >> def interp(x,y)
    >> "The sum of #{x} and #{y} is #{x+y}"
    >> end
    >>
    >> str = interp(210,27) # "The sum of 210 and 27 is 237"

    >
    >
    > Unless I'm mistaken, this recomputes the whole string every time
    > interp is called. I wanted to compute the common parts of the string,
    > cache it somewhere, and then compute the rest in another context.
    >
    > Hence, a 2-step interpolation.


    Yes, I'm sure there are some efficiency concerns. I've never
    measured the performance in a case like this.

    I wonder if it would be worthwhile to expose the internal
    method which does interpolation? I've thought of this in
    the past, but never seriously enough for an RCR. I think
    that would be "prettier" than an explicit eval.

    It would, of course, either have to operate in the current
    binding or have a binding passed in.


    Hal
    Hal Fulton, Feb 8, 2005
    #3
  4. Hal Fulton wrote:
    > I just wanted to make sure people don't overlook the
    > obvious:
    >
    >
    > def interp(x,y)
    > "The sum of #{x} and #{y} is #{x+y}"
    > end
    >
    > str = interp(210,27) # "The sum of 210 and 27 is 237"
    >
    >
    > Perhaps there are cases where this is no good though.
    >
    >
    > Hal


    * Patchworkstr = Struct.new( "Patchworkstring",:start,:middle,:end)
    * sentence = Patchworkstr.new( "There are " )
    *
    * sentence.end = " solutions."
    * n = 3**3
    * sentence["middle"] = n
    *
    * puts sentence.to_a.join

    ....prints
    There are 27 solutions.
    William James, Feb 8, 2005
    #4
    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. Aiden Humphreys

    Perl/Ruby string interpolation

    Aiden Humphreys, May 22, 2005, in forum: Java
    Replies:
    5
    Views:
    866
    Tor Iver Wilhelmsen
    May 22, 2005
  2. Paul Rubin
    Replies:
    4
    Views:
    292
    Michele Simionato
    Jan 19, 2004
  3. matt
    Replies:
    1
    Views:
    383
  4. matt
    Replies:
    8
    Views:
    214
    Mauricio Fernández
    Jun 19, 2004
  5. Navindra Umanee

    delayed string interpolation

    Navindra Umanee, Feb 7, 2005, in forum: Ruby
    Replies:
    13
    Views:
    211
    James Edward Gray II
    Feb 8, 2005
Loading...

Share This Page