Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > String interpolation

Reply
Thread Tools

String interpolation

 
 
Thomas Worm
Guest
Posts: n/a
 
      08-26-2007
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

 
Reply With Quote
 
 
 
 
Stefan Rusterholz
Guest
Posts: n/a
 
      08-26-2007
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/.

 
Reply With Quote
 
 
 
 
Thomas Worm
Guest
Posts: n/a
 
      08-26-2007
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

 
Reply With Quote
 
Xavier Noria
Guest
Posts: n/a
 
      08-26-2007
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


 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      08-26-2007
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet another string interpolation function... MonkeeSage Python 0 03-04-2007 08:38 AM
modifying html input date for mysql, reg ex or string interpolation? Kun Python 2 04-11-2006 11:17 PM
Perl/Ruby string interpolation Aiden Humphreys Java 5 05-22-2005 06:37 PM
yet another recipe on string interpolation Michele Simionato Python 8 11-08-2004 08:11 PM
YA string interpolation and printing idea Paul Rubin Python 4 01-19-2004 04:52 PM



Advertisments