String interpolation / hash of local variables

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?

The other, dirtier idea is something like this:

-------
template = %q{
<div>#{example}</div>
  ...
<div>%{foobar}</div>
}

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.

--
Posted via http://www.ruby-forum.com/.

Correction made, second block of demo code read #{foobar} not %{foobar}.

Any assistance appreciated

G.

···

--
Posted via http://www.ruby-forum.com/.

Hi,

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.

Please, this is not C.

···

--
Posted via http://www.ruby-forum.com/.

Try this:

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?

The other, dirtier idea is something like this:

-------
template = %q{
<div>#{example}</div>
...
<div>%{foobar}</div>
}

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.

--
Posted via http://www.ruby-forum.com/\.

There is a templating library in the stdlib called erb (
http://rdoc.info/stdlib/erb/ERB\)

require 'erb'

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

renderer = MyTemplateRenderer.new 'ExAmPlE', 'FoObAr'
renderer.render template # => " <div>ExAmPlE</div>\n <div>FoObAr</div>\n"

···

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?

The other, dirtier idea is something like this:

-------
template = %q{
<div>#{example}</div>
  ...
<div>%{foobar}</div>
}

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.

--
Posted via http://www.ruby-forum.com/\.

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?

The other, dirtier idea is something like this:

-------
template = %q{
  <div>#{example}</div>
   ...
  <div>%{foobar}</div>
}

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.

What about simply

> irb
1.9.3p125 :001 > template = "<div>%{example}</div>\n<div>%{foobar}</div>"
  => "<div>%{example}</div>\n<div>%{foobar}</div>"
1.9.3p125 :002 > arr =
  =>
1.9.3p125 :003 > magical_hash_with_local_variables = {}
  => {}
1.9.3p125 :004 > magical_hash_with_local_variables[:example] = "magical"
  => "magical"
1.9.3p125 :005 > magical_hash_with_local_variables[:foobar] = "hash"
  => "hash"
1.9.3p125 :006 > arr.push template % magical_hash_with_local_variables
  => ["<div>magical</div>\n<div>hash</div>"]

No eval. No external dependencies. In your loop you just populate your magical hash anew.

Sam

···

On 07/25/2012 03:08 PM, Gerbeck Shark wrote:

Nice! didn't know you could do that.

···

On Wed, Jul 25, 2012 at 3:14 PM, Sam Duncan <sduncan@wetafx.co.nz> wrote:

What about simply

> irb
1.9.3p125 :001 > template = "<div>%{example}</div>\n<div>%**
{foobar}</div>"
=> "<div>%{example}</div>\n<div>%**{foobar}</div>"
1.9.3p125 :002 > arr =
=>
1.9.3p125 :003 > magical_hash_with_local_**variables = {}
=> {}
1.9.3p125 :004 > magical_hash_with_local_**variables[:example] = "magical"
=> "magical"
1.9.3p125 :005 > magical_hash_with_local_**variables[:foobar] = "hash"
=> "hash"
1.9.3p125 :006 > arr.push template % magical_hash_with_local_**variables
=> ["<div>magical</div>\n<div>**hash</div>"]

No eval. No external dependencies. In your loop you just populate your
magical hash anew.