String interpolation

Discussion in 'Ruby' started by Thomas Worm, Aug 26, 2007.

  1. Thomas Worm

    Thomas Worm Guest

    a = 5
    b = "#{a}"
    puts b

    a = 6
    puts b

    Returns:
    5
    5

    which is clear to me, why. But is there a way to define such a string and
    interpolate it at a later time?

    Thomas
     
    Thomas Worm, Aug 26, 2007
    #1
    1. Advertising

  2. Thomas Worm wrote:
    > a = 5
    > b = "#{a}"
    > puts b
    >
    > a = 6
    > puts b
    >
    > Returns:
    > 5
    > 5
    >
    > which is clear to me, why. But is there a way to define such a string
    > and
    > interpolate it at a later time?
    >
    > Thomas


    a = 5
    b = proc { "a is now: #{a}" }
    def b.to_s; call; end
    puts b # !> a is now: 5
    a = 6
    puts b # !> a is now: 6

    Enjoy

    Regards
    Stefan
    --
    Posted via http://www.ruby-forum.com/.
     
    Stefan Rusterholz, Aug 26, 2007
    #2
    1. Advertising

  3. Thomas Worm

    Thomas Worm Guest

    On Sun, 26 Aug 2007 18:38:46 +0900, Stefan Rusterholz wrote:

    > a = 5
    > b = proc { "a is now: #{a}" }
    > def b.to_s; call; end
    > puts b # !> a is now: 5
    > a = 6
    > puts b # !> a is now: 6


    Many thanks for the quick help !!!
    Thomas
     
    Thomas Worm, Aug 26, 2007
    #3
  4. Thomas Worm

    Xavier Noria Guest

    On Aug 26, 2007, at 11:20 AM, Thomas Worm wrote:

    > a = 5
    > b = "#{a}"
    > puts b
    >
    > a = 6
    > puts b
    >
    > Returns:
    > 5
    > 5
    >
    > which is clear to me, why. But is there a way to define such a
    > string and
    > interpolate it at a later time?


    You normally use a templating system, for example:

    require 'erb'

    b = ERB.new("a is <%= a %>")

    a = 5
    puts b.result(binding) # -> a is 5

    a = 6
    puts b.result(binding) # -> a is 6

    -- fxn
     
    Xavier Noria, Aug 26, 2007
    #4
  5. Thomas Worm

    Phlip Guest

    Thomas Worm wrote:

    >a = 5
    > b = "#{a}"
    > puts b
    >
    > a = 6
    > puts b
    >
    > Returns:
    > 5
    > 5
    >
    > which is clear to me, why. But is there a way to define such a string and
    > interpolate it at a later time?


    You are asking how to do a "block closure". Study that, because it's a major
    Ruby topic and a very good design technique. I have not yet found a way to
    over-use or abuse blocks in Ruby!

    A 'lambda' is one of the block systems that can bond with the variables
    around it. So stick your string evaluator into a lambda, and call it:

    >> a = 5

    => 5
    >> q = lambda{"#{a}"}

    => #<Proc:0xb721d4d4@(irb):7>
    >> q.call

    => "5"
    >> a = 6

    => 6
    >> q.call

    => "6"

    Block closures are a very good design technique because a has a very limited
    scope over a very long lifespan. We could have stored that q and used it
    later. So a becomes very encapsulated.

    --
    Phlip
    http://www.oreilly.com/catalog/9780596510657/
    "Test Driven Ajax (on Rails)"
    assert_xpath, assert_javascript, & assert_ajax
     
    Phlip, Aug 26, 2007
    #5
    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:
    885
    Tor Iver Wilhelmsen
    May 22, 2005
  2. Paul Rubin
    Replies:
    4
    Views:
    302
    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:
    406
    Raymond Hettinger
    Nov 8, 2004
  4. Kun
    Replies:
    2
    Views:
    425
    John J. Lee
    Apr 12, 2006
  5. MonkeeSage
    Replies:
    0
    Views:
    283
    MonkeeSage
    Mar 4, 2007
Loading...

Share This Page