String interpolation method?

Discussion in 'Ruby' started by John Carter, Feb 27, 2008.

  1. John Carter

    John Carter Guest

    Ok, I'm being stupid probably...

    But I can't spot a method to do string interpolation.

    I have a string...

    a = 'bra#{c}ket'

    The variable c isn't available at the stage of setting that up. Hence
    the use of '' instead of "".

    The variable will be available later....

    At that stage I want to do something like...

    c=' see '
    a.interpolate

    The closest I can get is a bit fugly...

    eval "\"#{a}\""
    => "bra see ket"

    Any better way?

    Thanks,


    John Carter Phone : (64)(3) 358 6639
    Tait Electronics Fax : (64)(3) 359 4632
    PO Box 1645 Christchurch Email :
    New Zealand
     
    John Carter, Feb 27, 2008
    #1
    1. Advertising

  2. John Carter

    James Gray Guest

    On Feb 27, 2008, at 4:53 PM, John Carter wrote:

    > Ok, I'm being stupid probably...
    >
    > But I can't spot a method to do string interpolation.
    >
    > I have a string...
    >
    > a = 'bra#{c}ket'
    >
    > The variable c isn't available at the stage of setting that up.


    You want a templating solution, like the ERb code that ships with Ruby:

    #!/usr/bin/env ruby -wKU

    require "erb"

    template = ERB.new("bra<%= c %>ket")

    # and later...
    c = "c"
    p template.result

    __END__

    Hope that helps.

    James Edward Gray II
     
    James Gray, Feb 27, 2008
    #2
    1. Advertising

  3. John Carter

    yermej Guest

    On Feb 27, 4:53 pm, John Carter <> wrote:
    > Ok, I'm being stupid probably...
    >
    > But I can't spot a method to do string interpolation.
    >
    > I have a string...
    >
    > a = 'bra#{c}ket'
    >
    > The variable c isn't available at the stage of setting that up. Hence
    > the use of '' instead of "".
    >
    > The variable will be available later....
    >
    > At that stage I want to do something like...
    >
    > c=' see '
    > a.interpolate
    >
    > The closest I can get is a bit fugly...
    >
    > eval "\"#{a}\""
    > => "bra see ket"
    >
    > Any better way?
    >
    > Thanks,
    >
    > John Carter Phone : (64)(3) 358 6639
    > Tait Electronics Fax : (64)(3) 359 4632
    > PO Box 1645 Christchurch Email :
    > New Zealand


    I would do it thusly:

    a = "bra%sket"
    c = ' see '
    a % c
    => "bra see ket"
     
    yermej, Feb 27, 2008
    #3
  4. John Carter

    Trans Guest

    => require 'facets/string/interpolate'
    => true
    >> a = 'bra#{c}ket'

    => "bra\#{c}ket"
    >> c = 4

    => 4
    >> String.interpolate{a}

    => "bra4ket"

    T.
     
    Trans, Feb 28, 2008
    #4
  5. John Carter

    Guby Guest

    Not that it is a lot nicer, but just another solution... and a little
    bit on side of what you wanted, but:

    >> a = lambda{|c| "bra#{c}ket"}
    >> a.call(" see ")

    => "bra see ket"

    :)


    On Feb 28, 2008, at 12:42 AM, John Carter wrote:

    > On Thu, 28 Feb 2008, Trans wrote:
    >
    >
    >> => require 'facets/string/interpolate'

    >
    > Hmm....
    >
    > Very neat in application....
    >
    >>>> String.interpolate{a}

    >
    > but....
    >
    > def interpolate(&str)
    > eval "%{#{str.call}}", str.binding
    > end
    >
    > ...equally nasty in implementation.
    >
    > Ah well, clearly then I wasn't missing something.... there is no
    > standard method.
    >
    > Thanks,
    >
    >
    > John Carter Phone : (64)(3) 358 6639
    > Tait Electronics F
     
    Guby, Feb 29, 2008
    #5
  6. If you want it to look even nicer, write it like:
    >> a = lambda{|c| "bra#{c}ket"}
    >> a[" see "]

    => "bra see ket"

    -Rob

    On Feb 29, 2008, at 7:21 AM, Guby wrote:
    > Not that it is a lot nicer, but just another solution... and a
    > little bit on side of what you wanted, but:
    >
    > >> a = lambda{|c| "bra#{c}ket"}
    > >> a.call(" see ")

    > => "bra see ket"
    >
    > :)
    >
    > On Feb 28, 2008, at 12:42 AM, John Carter wrote:
    >
    >> On Thu, 28 Feb 2008, Trans wrote:
    >>
    >>
    >>> => require 'facets/string/interpolate'

    >>
    >> Hmm....
    >>
    >> Very neat in application....
    >>
    >>>>> String.interpolate{a}

    >>
    >> but....
    >>
    >> def interpolate(&str)
    >> eval "%{#{str.call}}", str.binding
    >> end
    >>
    >> ...equally nasty in implementation.
    >>
    >> Ah well, clearly then I wasn't missing something.... there is no
    >> standard method.
    >>
    >> Thanks,
    >>
    >> John Carter Phone : (64)(3) 358 6639
    >> Tait Electronics F


    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Feb 29, 2008
    #6
  7. John Carter

    Guby Guest

    Sweet, that looks better!
    Good to learn something new! :)

    S

    On Feb 29, 2008, at 11:07 AM, Rob Biedenharn wrote:

    >
    > If you want it to look even nicer, write it like:
    > >> a = lambda{|c| "bra#{c}ket"}
    > >> a[" see "]

    > => "bra see ket"
    >
    > -Rob
    >
    > On Feb 29, 2008, at 7:21 AM, Guby wrote:
    >> Not that it is a lot nicer, but just another solution... and a
    >> little bit on side of what you wanted, but:
    >>
    >> >> a = lambda{|c| "bra#{c}ket"}
    >> >> a.call(" see ")

    >> => "bra see ket"
    >>
    >> :)
    >>
    >> On Feb 28, 2008, at 12:42 AM, John Carter wrote:
    >>
    >>> On Thu, 28 Feb 2008, Trans wrote:
    >>>
    >>>
    >>>> => require 'facets/string/interpolate'
    >>>
    >>> Hmm....
    >>>
    >>> Very neat in application....
    >>>
    >>>>>> String.interpolate{a}
    >>>
    >>> but....
    >>>
    >>> def interpolate(&str)
    >>> eval "%{#{str.call}}", str.binding
    >>> end
    >>>
    >>> ...equally nasty in implementation.
    >>>
    >>> Ah well, clearly then I wasn't missing something.... there is no
    >>> standard method.
    >>>
    >>> Thanks,
    >>>
    >>> John Carter Phone : (64)(3) 358 6639
    >>> Tait Electronics F

    >
    > Rob Biedenharn http://agileconsultingllc.com
    >
    >
    >
    >
     
    Guby, Feb 29, 2008
    #7
  8. John Carter

    Trans Guest

    On Feb 29, 9:07 am, Rob Biedenharn <>
    wrote:
    > If you want it to look even nicer, write it like:
    > >> a = lambda{|c| "bra#{c}ket"}
    > >> a[" see "]

    > => "bra see ket"


    The only trouble here is that you get the binding of where you defined
    the lambda, rather than the one in which you ultimately evaluate it
    in.

    With 1.9 I think we can use instance_exec to handle that however --
    that being the case, the String::interpolate method I demoed above
    could be improved.

    T.
     
    Trans, Feb 29, 2008
    #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. Aiden Humphreys

    Perl/Ruby string interpolation

    Aiden Humphreys, May 22, 2005, in forum: Java
    Replies:
    5
    Views:
    876
    Tor Iver Wilhelmsen
    May 22, 2005
  2. Paul Rubin
    Replies:
    4
    Views:
    300
    Michele Simionato
    Jan 19, 2004
  3. Michele Simionato

    yet another recipe on string interpolation

    Michele Simionato, Nov 4, 2004, in forum: Python
    Replies:
    8
    Views:
    401
    Raymond Hettinger
    Nov 8, 2004
  4. Kun
    Replies:
    2
    Views:
    422
    John J. Lee
    Apr 12, 2006
  5. MonkeeSage
    Replies:
    0
    Views:
    277
    MonkeeSage
    Mar 4, 2007
Loading...

Share This Page