How to do this

D

Damjan Rems

I don't know how to explain so I will just code:

template = 'some text #{variable}' # single quotes for reason
variable = 'filled'
res = 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 inserting text with my own method.

by
TheR
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

I don't know how to explain so I will just code:

template = 'some text #{variable}' # single quotes for reason
variable = 'filled'
res = 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 inserting text with my own method.

by
TheR

Using ERB, here is a pretty straight forward cheatsheet/examples for you:
http://github.com/JoshCheek/JoshsRubyKickstart/blob/master/cheatsheets/erb-embedded_ruby.rb

require 'erb'

def some_method(template)
ERB.new( template , 0 , '>' ).result
end

template = 'some text <%= variable %>'
variable = 'filled'
res = some_method(template)

res # => "some text filled"
 
R

Robert Klemme

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/
 
S

Stefano Crocco

|I don't know how to explain so I will just code:
|
|template = 'some text #{variable}' # single quotes for reason
|variable = 'filled'
|res = 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 inserting text with my own method.
|
|by
|TheR

You could do this:

#note the double quotes inside the single ones
template = '"some text #{variable}"'
variable = 'filled'
res = eval template

A better way could be to use ERB (included in the standard library):
require 'erb'
template = ERB.new "some text <%= variable %>"
variable = 'filled'
res = template.result

I hope this helps

Stefano
 
D

Damjan Rems

Robert Klemme wrote in post #949495:
Btw, parsing can be as easy as

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

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


Robert thank you for this.

Ruby really is an amazing language. And after five years I still get
puzzled how some things can be so simple.


by
TheR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,144
Messages
2,570,823
Members
47,369
Latest member
FTMZ

Latest Threads

Top