From: "Ara.T.Howard" Date: 2005-07-16T23:19:31+09:00 Subject: Re: delaying string evaluation On Sat, 16 Jul 2005, Navindra Umanee wrote: > Hi, > > I would like to partially expand a string and then further expand it > later on when more information is available. I am currently using > string interpolation with a reeval method suggested by Bill Kelly. > > class String > def reeval(b = TOPLEVEL_BINDING) > eval("< end > end > > This lets me do stuff like this: > > expanded = "one" > delayed = "two" > > s = "#{expanded} \#{delayed}" # => "one #{delayed}" > s = s.reeval(binding) # => "one two" why not make up a mini interpolator? ~ > cat a.rb class String def expand! h = {} gsub!(%r/:\w+/o){|k| h[k.delete(':').intern] || k} end def expand(*a, &b) dup.expand!(*a, &b); self end end s = '<:expanded> - <:delayed>' s.expand! :expanded => 'one' puts s s.expand! :delayed => 'two' puts s ~ > ruby a.rb - <:delayed> - cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================