Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Delayed string interpolation

Reply
Thread Tools

Delayed string interpolation

 
 
Hal Fulton
Guest
Posts: n/a
 
      02-08-2005
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



 
Reply With Quote
 
 
 
 
Navindra Umanee
Guest
Posts: n/a
 
      02-08-2005
Hal Fulton <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
 
Hal Fulton
Guest
Posts: n/a
 
      02-08-2005
Navindra Umanee wrote:
> Hal Fulton <(E-Mail Removed)> 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




 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      02-08-2005

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.

 
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
Perl/Ruby string interpolation Aiden Humphreys Java 5 05-22-2005 06:37 PM
delayed string interpolation Navindra Umanee Ruby 13 02-08-2005 01:59 PM
Delayed Instantiation - delayed.rb matt Ruby 8 06-19-2004 08:08 PM
Delayed Instantiation - delayed.rb matt Ruby 1 06-17-2004 01:43 AM
YA string interpolation and printing idea Paul Rubin Python 4 01-19-2004 04:52 PM



Advertisments