On Tue, Oct 12, 2010 at 3:02 PM, Damjan Rems <d_rems / yahoo.com> wrote:
> I don't know how to explain so I will just code:
>
> template =3D 'some text #{variable}' # single quotes for reason
> variable =3D 'filled'
> res =3D some_method(template)
>
> Here, res should have value 'some text filled'.
>
> Is there an easy way doing this in ruby. Hard way is of course parsing
> and =A0inserting text with my own method.

I would consider using double quotes an "easy way".  Either way, some
piece of code needs to to the parsing.  If you only ever want to fill
in a single value you could as well use (s)printf.

Btw, parsing can be as easy as

def replace(template, values)
  template.gsub /(?<!\\)#\{([^}]*)\}/ do
    values[$1]
  end
end

template =3D 'foo \\#{bar}-#{bingo}-#{bongo}'
x =3D replace template, "bar" =3D> "1", "bongo" =3D> "2"
p template, x

Cheers

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/