Can erb produce erb?


#1

just wondering if you could do something like this…

ERB.new( some_erb_string ).result # ‘<%= result %>’ another erb
template

so that way the result could be processed by another ERB.new


#2

On 4/6/07, talkin ruby removed_email_address@domain.invalid wrote:

just wondering if you could do something like this…

ERB.new( some_erb_string ).result # ‘<%= result %>’ another erb template

so that way the result could be processed by another ERB.new

You are an evil and twisted individual! :wink:

To answer your question, though, sure! ERb can do that.

require ‘erb’

@blah = ‘<%= @not_blah %>’
ERB.new( “blah <%= @blah %>” ).result #=> “blah <%= @not_blah %>”

Blessings,
TwP


#3

@maybe= val.nil? ? true : false

blah <%%= <%= @maybe %> %>

I use this format in generators for rails. I dont know if it is as
clean
looking for short example but for full rhtml pages it works well.

Stephen B. IV

On 4/6/07, Tim P. removed_email_address@domain.invalid wrote:

On 4/6/07, talkin ruby removed_email_address@domain.invalid wrote:

just wondering if you could do something like this…

ERB.new( some_erb_string ).result # ‘<%= result %>’ another erb
template


#4

On Sat, 07 Apr 2007 04:07:16 +0900, Tim P. wrote:

To answer your question, though, sure! ERb can do that.

require ‘erb’

@blah = ‘<%= @not_blah %>’
ERB.new( “blah <%= @blah %>” ).result #=> “blah <%= @not_blah %>”

If you really want to be evil and twisted, what’s the smallest self-
reproducing erb program you can write that doesn’t read its own file.

The following solution is illegal:
<%= open(FILE){|f| f.read} %>

–Ken


#5

thanks! that is exactly what I needed!


#6

Ken B. wrote:

reproducing erb program you can write that doesn’t read its own file.

The following solution is illegal:
<%= open(FILE){|f| f.read} %>
I think you mean “what’s the smallest non-trivial self-reproducing erb
program…”:

irb(main):012:0> ERB.new(’ ‘).result
=> " "
irb(main):013:0> ERB.new(’’).result
=> “”

:slight_smile:


#7

Ken B. wrote:

reproducing erb program you can write that doesn’t read its own file.

The following solution is illegal:
<%= open(FILE){|f| f.read} %>

Drat. This nearly works:

<%=s=";"<%=s=#{s.inspect}#{s}"%>";"<%=s=#{s.inspect}#{s}"%>

The only reason it doesn’t is that ERB barfs on ‘%>’ in a string inside
a <%= %> block, while String#inspect ignores it. This requires…
trickery.

Luckily, we have trickery on hand. When in doubt, reverse the data:

<%=s=">%"}esrever.s{#}tcepsni.s{#=s=%<";";"<%=s=#{s.inspect}#{s.reverse}"%>

That’s 77 characters by my count. 77 characters of pure, twisted, evil
:slight_smile:


#8

Ken B. removed_email_address@domain.invalid writes:

ERB.new( “blah <%= @blah %>” ).result #=> “blah <%= @not_blah %>”
=> " "
irb(main):013:0> ERB.new(’’).result
=> “”

Come to think of it, ERB is way too easy a language. You have to define
non-trivial to mean “includes a <% and a %>”

irb(main):001:0> ERB.new(’>%%<’).result
=> “>%%<”


#9

On Sun, 08 Apr 2007 16:30:11 +0900, Alex Y. wrote:

If you really want to be evil and twisted, what’s the smallest self-
=> “”
Come to think of it, ERB is way too easy a language. You have to define
non-trivial to mean “includes a <% and a %>”


#10

On 4/9/07, Christian N. removed_email_address@domain.invalid wrote:

Hey, that doesn’t count! It doesn’t contain ‘<%’ or ‘%>’. How about
this:

<%=File.new($FILENAME).readlines%>