Forum: Ruby Delayed quote expansion

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-18 07:06
I'm sure the answer to this will seem simple once I see it.  Right now
it eludes me.

How can one have a string object like this...

a = '#{some_value}'

and have it get evaluated as a double quoted string (with the value for
some_value being automatically replaced) at a later time?

I would prefer that it is evaluated in context from which it is called.

I would like to do something like this...

def func(param)
  test = 42
  puts param.quote_substitution
end

func('my number is #{test}')    #=> "my number is 42"

_Kevin
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 unknown (Guest)
on 2006-01-18 08:14
(Received via mailing list)
On Jan 18, 2006, at 12:29 AM, Kevin Olbrich wrote:
> func('my number is #{test}')    #=> "my number is 42"
How about:

	template = lambda { |x| "my number is: #{x}" }

	# some time passes

	template.call(42)	# "my number is: 42"




Gary Wright
5c7bdd14d6885c8275eaf78be41d120a?d=identicon&s=25 Eero Saynatkari (Guest)
on 2006-01-18 09:29
(Received via mailing list)
On 2006.01.18 14:29, Kevin Olbrich wrote:
> I would prefer that it is evaluated in context from which it is called.
>
> I would like to do something like this...
>
> def func(param)
>   test = 42
>   puts param.quote_substitution
> end
>
> func('my number is #{test}')    #=> "my number is 42"

You can delay evaluation like this:

  string = '"#{foo}"'

  # ...

  value  = eval string   # Add a binding if you want

> _Kevin


E
22610bf26fbc5bd949c8ba4646ceb006?d=identicon&s=25 Paolo Capriotti (Guest)
on 2006-01-18 11:48
(Received via mailing list)
On 1/18/06, Kevin Olbrich <kevin.olbrich@duke.edu> wrote:
[snip]
> I would like to do something like this...
>
> def func(param)
>   test = 42
>   puts param.quote_substitution
> end
>
> func('my number is #{test}')    #=> "my number is 42"

Try this:

class String
  def evaluate(*args)
    eval inspect.gsub(/\\#/, '#'), *args
  end
end

def func(str)
  x = 42
  str.evaluate(binding)
end

puts func('the answer is #{x}')



--

Paolo
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-18 14:24
Lots of good responses here, but the one that turns out to be the
easiest is...

def func(param)
  some_value = 42
  value = eval %Q{%Q{#{param}}}
end

func('my number is #{some_value}')  #=> 'my number is 42'


_Kevin
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-01-18 15:19
(Received via mailing list)
On Jan 18, 2006, at 6:48 AM, Kevin Olbrich wrote:

> Lots of good responses here, but the one that turns out to be the
> easiest is...
>
> def func(param)
>   some_value = 42
>   value = eval %Q{%Q{#{param}}}
> end
>
> func('my number is #{some_value}')  #=> 'my number is 42'

What you really want here is ERB:

 >> require "erb"
=> true
 >> def expand( template )
 >>   some_value = 42
 >>   ERB.new(template).result(binding)
 >> end
=> nil
 >> expand "My number is <%= some_value %>."
=> "My number is 42."

James Edward Gray II
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-18 15:28
James Gray wrote:
> What you really want here is ERB:
>

A good suggestion, but I would rather avoid unnecessary dependencies if
there is an adequate pure ruby way to do it.

_Kevin
123320fdc17940dfc8e365edb48fbff2?d=identicon&s=25 Bob Showalter (Guest)
on 2006-01-18 15:44
(Received via mailing list)
Kevin Olbrich wrote:
> James Gray wrote:
>
>>What you really want here is ERB:
>>
>
>
> A good suggestion, but I would rather avoid unnecessary dependencies if
> there is an adequate pure ruby way to do it.

erb *is* pure Ruby. It is part of the standard library, so every Ruby
installation has it.
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-18 17:49
Bob Showalter wrote:

> erb *is* pure Ruby. It is part of the standard library, so every Ruby
> installation has it.

So it is, my bad.
This topic is locked and can not be replied to.