-------
template = <<-HTML
<div>%{example}</div>
...
<div>%{foobar}</div>
HTML
for i in special_list
example = ....
foobar = ...
arr.push template % return_magical_hash_with_local_variables()
-------
in Python there is a locals() function that returns a dictionary/hash of
local variables.
In Ruby I've found local_variables() but that does not provide the
values. It should for just the example above! Or perhaps I can be
enlightened by someone here?
for i in special_list
example = ....
foobar = ...
arr.push eval("\"template\"")
-------
I believe one should avoid calling eval as much as possible. I tried the
"Facets" package,
"String.interpolate{template}" calls eval, it just masks the fact.
This could be avoided if there were a function that return a dictionary
or hash of local variables.
I find this just dangerous and ugly, and I see no reason why anyone
would want to use this. At best it's a demonstration how to create
security problems and confuse readers with unnecessary magic.
The practical solution for this is to simply save the values in a hash
-- or even better, use a template engine.
z = 1
y = 2
hash = Hash.new
local_variables.each do |var|
hash[var] = eval var
end
puts h
That should give you a hash of the local variables.
···
On Jul 24, 2012, at 9:08 PM, Gerbeck Shark wrote:
Hello all-
I'm trying to do something likes
Demonstration Ruby/logic
-------
template = <<-HTML
<div>%{example}</div>
...
<div>%{foobar}</div>
HTML
for i in special_list
example = ....
foobar = ...
arr.push template % return_magical_hash_with_local_variables()
-------
in Python there is a locals() function that returns a dictionary/hash of
local variables.
In Ruby I've found local_variables() but that does not provide the
values. It should for just the example above! Or perhaps I can be
enlightened by someone here?
for i in special_list
example = ....
foobar = ...
arr.push eval("\"template\"")
-------
I believe one should avoid calling eval as much as possible. I tried the
"Facets" package,
"String.interpolate{template}" calls eval, it just masks the fact.
This could be avoided if there were a function that return a dictionary
or hash of local variables.
template = <<-HTML
<div><%= example %></div>
<div><%= foobar %></div>
HTML
# ===== simple example with toplevel locals =====
example = 'EXAMPLE'
foobar = 'FOOBAR'
ERB.new(template).result binding # => " <div>EXAMPLE</div>\n
<div>FOOBAR</div>\n"
# ===== more realistic example with methods on an object =====
# you can technically do ivars in these, too, which
# is what most web frameworks use, but that sucks IMO
class MyTemplateRenderer
attr_accessor :example, :foobar
def initialize(example, foobar)
self.example, self.foobar = example, foobar
end
def render(template)
ERB.new(template).result binding
end
end
On Tue, Jul 24, 2012 at 10:08 PM, Gerbeck Shark <lists@ruby-forum.com>wrote:
Hello all-
I'm trying to do something likes
Demonstration Ruby/logic
-------
template = <<-HTML
<div>%{example}</div>
...
<div>%{foobar}</div>
HTML
for i in special_list
example = ....
foobar = ...
arr.push template % return_magical_hash_with_local_variables()
-------
in Python there is a locals() function that returns a dictionary/hash of
local variables.
In Ruby I've found local_variables() but that does not provide the
values. It should for just the example above! Or perhaps I can be
enlightened by someone here?
for i in special_list
example = ....
foobar = ...
arr.push eval("\"template\"")
-------
I believe one should avoid calling eval as much as possible. I tried the
"Facets" package,
"String.interpolate{template}" calls eval, it just masks the fact.
This could be avoided if there were a function that return a dictionary
or hash of local variables.
-------
template =<<-HTML
<div>%{example}</div>
...
<div>%{foobar}</div>
HTML
for i in special_list
example = ....
foobar = ...
arr.push template % return_magical_hash_with_local_variables()
-------
in Python there is a locals() function that returns a dictionary/hash of
local variables.
In Ruby I've found local_variables() but that does not provide the
values. It should for just the example above! Or perhaps I can be
enlightened by someone here?
for i in special_list
example = ....
foobar = ...
arr.push eval("\"template\"")
-------
I believe one should avoid calling eval as much as possible. I tried the
"Facets" package,
"String.interpolate{template}" calls eval, it just masks the fact.
This could be avoided if there were a function that return a dictionary
or hash of local variables.