Single quoted string to become double quoted - is it possible?

Hello, Ruby experts.
Imagine I have single quoted string:
a = 'bla bla #{name} bla bla'
name = 'Andrew'
Is it possible to make a act as double quoted string: puts a => 'bla bla Andrew bla bla' instead of' bla bla #{name} bla bla'?

This is terrible, but:

eval "\"#{a}\""

Why do you need to use single quotes? If you just want to avoid using
double quotes (perhaps because you want double quotes within the string),
you could use something like this instead:

    name = 'Andrew'
    puts %Q{blah blah #{name} blah blah}

If you want to be able to reuse the string, you could define a method:

    def a(name_input)
      %Q{blah blah #{name_input} blah blah}
    end

    name = 'Andrew'

    puts a(name)

···

On Sun, Dec 18, 2011 at 05:20:19AM +0900, ANDREW BIZYAEV (GMAIL) wrote:

Hello, Ruby experts.
Imagine I have single quoted string:
a = 'bla bla #{name} bla bla'
name = 'Andrew'
Is it possible to make a act as double quoted string: puts a => 'bla
bla Andrew bla bla' instead of' bla bla #{name} bla bla'?

Thank you, Steve. It works fine.

···

On 17.12.2011, at 23:33, Steve Klabnik wrote:

This is terrible, but:

eval "\"#{a}\""

Hello, Ruby experts.
Imagine I have single quoted string:
a = 'bla bla #{name} bla bla'
name = 'Andrew'
Is it possible to make a act as double quoted string: puts a => 'bla
bla Andrew bla bla' instead of' bla bla #{name} bla bla'?

Why do you need to use single quotes?

I use yaml configuration file. in this file I set string template like this:
:event:
  :template: "bla bla #{name} bla bla" #in order to be flexible

Then I load this yaml into @options variable and define @template:
@template = @options[:event][:template] #=> "bla bla #{name} bla bla" So the effect is the same as using single quotes. And it seems I cannot change the way Ruby reads yaml strings.
Later in my program I want to use the @template to generate string with real value of name.
I use Steve's approach and it works fine:
a = eval "/"#{template}/"" # => "bla bla Andrew bla bla"

If you just want to avoid using
double quotes (perhaps because you want double quotes within the string),
you could use something like this instead:

   name = 'Andrew'
   puts %Q{blah blah #{name} blah blah}

If you want to be able to reuse the string, you could define a method:

   def a(name_input)
     %Q{blah blah #{name_input} blah blah}
   end

   name = 'Andrew'

   puts a(name)

Thank you for advice. You see I have some other case.

···

On 18.12.2011, at 0:03, Chad Perrin wrote:

On Sun, Dec 18, 2011 at 05:20:19AM +0900, ANDREW BIZYAEV (GMAIL) wrote:

Use a templating language instead:

data = '
:event:
:template: "bla bla <%= name %> bla bla" #in order to be flexible
'

require 'yaml'
yaml = YAML.load data
yaml # => {:event=>{:template=>"bla bla <%= name %> bla bla"}}
to_parse = yaml[:event][:template] # => "bla bla <%= name %> bla bla"

require 'erb'
name = "Jefferson"
ERB.new(to_parse).result # => "bla bla Jefferson bla bla"

# note that you may have to pass a binding depending on how you want to use
it.

···

On Sat, Dec 17, 2011 at 3:29 PM, ANDREW BIZYAEV (GMAIL) < andrew.bizyaev@gmail.com> wrote:

On 18.12.2011, at 0:03, Chad Perrin wrote:

> On Sun, Dec 18, 2011 at 05:20:19AM +0900, ANDREW BIZYAEV (GMAIL) wrote:
>> Hello, Ruby experts.
>> Imagine I have single quoted string:
>> a = 'bla bla #{name} bla bla'
>> name = 'Andrew'
>> Is it possible to make a act as double quoted string: puts a => 'bla
>> bla Andrew bla bla' instead of' bla bla #{name} bla bla'?
>
> Why do you need to use single quotes?
I use yaml configuration file. in this file I set string template like
this:
:event:
:template: "bla bla #{name} bla bla" #in order to be flexible

Then I load this yaml into @options variable and define @template:
@template = @options[:event][:template] #=> "bla bla #{name} bla bla" So
the effect is the same as using single quotes. And it seems I cannot change
the way Ruby reads yaml strings.
Later in my program I want to use the @template to generate string with
real value of name.
I use Steve's approach and it works fine:
a = eval "/"#{template}/"" # => "bla bla Andrew bla bla"

Yeah, I see that now. I think, for that use case, I'd probably do some
parse-and-replace on any strings loaded from the YAML, rather than eval
every string from YAML -- it's safer. Any time you think eval is the
right answer to something, it pays to think three or four more times
before you decide you're sure, *especially* if there's any possibility of
arbitrary data getting into what you're going to eval.

···

On Sun, Dec 18, 2011 at 06:29:11AM +0900, ANDREW BIZYAEV (GMAIL) wrote:

Then I load this yaml into @options variable and define @template:
@template = @options[:event][:template] #=> "bla bla #{name} bla bla" So the effect is the same as using single quotes. And it seems I cannot change the way Ruby reads yaml strings.
Later in my program I want to use the @template to generate string with real value of name.
I use Steve's approach and it works fine:
a = eval "/"#{template}/"" # => "bla bla Andrew bla bla"

> If you just want to avoid using
> double quotes (perhaps because you want double quotes within the string),
> you could use something like this instead:
>
> name = 'Andrew'
> puts %Q{blah blah #{name} blah blah}
>
> If you want to be able to reuse the string, you could define a method:
>
> def a(name_input)
> %Q{blah blah #{name_input} blah blah}
> end
>
> name = 'Andrew'
>
> puts a(name)
>
Thank you for advice. You see I have some other case.

Use a templating language instead:

Or a string substitution:

  string = "blah blah blah NAME blah blah blah"
  name = "Andrew"
  string.sub! "NAME", name