Erb.rb $B$N;EMM$K$D$$$F(B

e$B7,ED$H$$$$$^$9!#e(B
erb.rb e$B$K$D$$$F<ALd$G$9!#e(B

  1. ERB#result() e$B$G!"e(Bsafe_level e$B$,@_Dj$5$l$F$$$k$H$-$Ke(B
    Thread e$B$r@8@.$7$F$+$i$=$NCf$Ge(B eval e$B$7$F$$$^$9$,!"e(B
    Thread e$B$r@8@.$9$kM}M3$r65$($F$$$?$@$1$^$;$s$+!#e(B

  2. e$B0J2<$N%a%=%C%I$d%b%8%e!<%k$Oe(B #:nodoc: e$B$,$D$$$F$^$9$,!“e(B
    e$B$3$l$i$OHs8x3+$G$”$j@Q6KE*$K;H$&$Y$-$G$J$$$H9M$($Fe(B
    e$B$$$$$G$7$g$&$+!#e(B
    e$B$b$C$H$$$&$H!"$3$l$i$re(BERBe$B$N;EMM$H9M$($F$$$$$+$I$&$+$He(B
    e$B$$$&$3$H$J$s$G$9$,!#e(B

    • ERB#def_method
    • ERB#def_module
    • ERB#def_class
    • ERB::DefMethod
  3. ERB#set_eoutvar() e$B$C$FI,MW$G$7$g$&$+!)e(B
    ERB#initialize() e$B$N0z?te(B _eoutvar e$B$r;H$($PJQ?tL>e(B _erbout
    e$B$Oe(B
    e$BJQ99$G$-$k$+$i!"e(Bset_eoutvar() e$B$OI,MW$J$$$h$&$K;W$$$^$9!#e(B

e$B0J>e$G$9!#$h$m$7$/$*4j$$$7$^$9!#e(B

e$B31$H$$$$$^$9!#e(B

On 2008/02/09, at 17:50, Makoto Kuwata wrote:

  1. ERB#result() e$B$G!"e(Bsafe_level e$B$,@_Dj$5$l$F$$$k$H$-$Ke(B
    Thread e$B$r@8@.$7$F$+$i$=$NCf$Ge(B eval e$B$7$F$$$^$9$,!"e(B
    Thread e$B$r@8@.$9$kM}M3$r65$($F$$$?$@$1$^$;$s$+!#e(B

e$B$?$7$+!“e(B$SAFEe$B$O$h$jBg$-$JCM$7$+@_Dj$G$-$J$$$+$i$@$C$?$He(B
e$B;W$$$^$9!#e(B
e$B8F$S=P$785$Ne(B$SAFEe$B$,JQ$o$C$F$7$^$&$N$rHr$1$k$?$a$G$9$M!#e(B
e$B:#$J$i$b$C$H$&$^$$J}K!$,$”$k$+$b$7$l$^$;$s!#e(B

  • ERB::DefMethod
    e$B$3$l$i$O@Q6KE*$K;H$C$F$[$7$$%a%=%C%I$G$9!#e(B

rdoce$B$K$D$$$F$O$I$J$?$+$,=q$$$F$/$l$?$b$N$G!“8x3+e(B(?)e$B$H4Xe(B
e$BO”$O$"$j$^$;$s!#e(B

e$B5$J,E*$K$O!“e(BERB::Compilere$B4XO”$,Hs8x3+!J$H$$$C$F$be(B
Rubye$B$J$N$G<B<A8x3+!K$G$9!#e(B
ERB::Compilere$B$r2~B$$7$F$bE$i$l$J$$$@$m$&$J$“!”$H4|BT$7$F$^$9!#e(B

  1. ERB#set_eoutvar() e$B$C$FI,MW$G$7$g$&$+!)e(B
    ERB#initialize() e$B$N0z?te(B _eoutvar e$B$r;H$($PJQ?tL>e(B _erbout
    e$B$Oe(B
    e$BJQ99$G$-$k$+$i!"e(Bset_eoutvar() e$B$OI,MW$J$$$h$&$K;W$$$^$9!#e(B

e$B$9$C$+$jK:$l$F$$$^$7$?$,!"e(B_erboute$B$OJQ?tL>$7$+@_Dj$G$-$Je(B
e$B$$$+$ie(B
e$B%a%=%C%I$K$7$F%U%C%/$G$-$k$h$&$K$7$?$h$&$G$9!#e(B

e$B;d$b7,ED$5$s$K<ALd$,$"$j$^$9!#e(B

erbe$B$de(Berubye$B$O!"<B9T;~%(%i!<$N9THV9f$N>pJs$,$o$+$k$h$&$Ke(B
e$B85$Ne(BeRubye$B%9%/%j%W%H$N9T$r0];}$7$?%9%/%j%W%H$KJQ49$7$F$$e(B
e$B$^$9!#e(B

RubyKaigie$B$N9V1i$G$OO"B3$7$?J8;zNs$O!"9T$r$^$?$$$G$$$F$be(B
e$B$D$J$2$F$7$^$&$h$&$K8+$($?$N$G$9$,!"e(Berubise$BJ}LL$G$O$=$&$$$C$?e(B
e$B%K!<%:$O$J$$$N$G$7$g$&$+e(B?

e$B31$H$$$$$^$9!#e(B

On 2008/02/11, at 3:02, Makoto Kuwata wrote:

e$B$J$K$+<ALd$N0U?^$r4V0c$C$F$$$?$i$9$_$^$;$s!#e(B

e$B$$$d;d$N4*0c$$$G$7$?!#e(B
e$B$J$K$+%@%a$J%Q%?!<%s$r8+$D$1$?5$$,$7$F$?$s$@$1$I!#e(B

e$B7,ED$G$9!#e(B
e$B2sEz$"$j$,$H$&$4$6$$$^$9!#e(B

2008/2/10 Masatoshi SEKI [email protected]:

  1. ERB#result() e$B$G!"e(Bsafe_level e$B$,@_Dj$5$l$F$$$k$H$-$Ke(B
    Thread e$B$r@8@.$7$F$+$i$=$NCf$Ge(B eval e$B$7$F$$$^$9$,!"e(B
    Thread e$B$r@8@.$9$kM}M3$r65$($F$$$?$@$1$^$;$s$+!#e(B

e$B$?$7$+!“e(B$SAFEe$B$O$h$jBg$-$JCM$7$+@_Dj$G$-$J$$$+$i$@$C$?$He(B
e$B;W$$$^$9!#e(B
e$B8F$S=P$785$Ne(B$SAFEe$B$,JQ$o$C$F$7$^$&$N$rHr$1$k$?$a$G$9$M!#e(B
e$B:#$J$i$b$C$H$&$^$$J}K!$,$”$k$+$b$7$l$^$;$s!#e(B

e$B$3$A$i$K=q$$$F$"$j$^$7$?!#e(B
http://www.ruby-lang.org/ja/man/html/_A5BBA5ADA5E5A5EAA5C6A5A3A5E2A5C7A5EB.html
$SAFE e$B$C$F!"IaDL$N%0%m!<%P%kJQ?t$@$H;W$C$F$^$7$?$,!"e(B
e$B$=$&$G$O$J$$$s$G$9$M!#e(B

e$B$=$l$G$O!"e(BThread e$B$r;H$&$+$I$&$+$O;EMM$G$O$J$/!“e(B
e$B!Ve(Bsafe_level e$B$r@_Dj$7$?$H$-$K$=$l$,%a%$%s%W%m%0%i%`$Ne(B
$SAFE e$B$K1F6A$r5Z$$5$J$$$3$H!W$,;EMM$@$H$$$&M}2r$Ge(B
e$B$h$m$7$$$G$7$g$&$+!#e(B
e$B$b$7$=$&$J$i!”<!$N$h$&$K$9$l$Pe(B Thread e$B$N@8@.$r>/$J$/e(B
e$B$G$-$F$$$$$N$+$J$H;W$$$^$7$?!#e(B

Index: lib/erb.rb

— lib/erb.rb (revision 15382)
+++ lib/erb.rb (working copy)
@@ -733,7 +733,7 @@

code evaluation.

def result(b=TOPLEVEL_BINDING)

  • if @safe_level
  • if @safe_level && @safe_level > $SAFE
    th = Thread.start {
    $SAFE = @safe_level
    eval(@src, b, (@filename || ‘(erb)’), 1)
    ===================================================================

e$B$3$l$i$O@Q6KE*$K;H$C$F$[$7$$%a%=%C%I$G$9!#e(B

rdoce$B$K$D$$$F$O$I$J$?$+$,=q$$$F$/$l$?$b$N$G!“8x3+e(B(?)e$B$H4Xe(B
e$BO”$O$"$j$^$;$s!#e(B

e$B5$J,E*$K$O!“e(BERB::Compilere$B4XO”$,Hs8x3+!J$H$$$C$F$be(B
Rubye$B$J$N$G<B<A8x3+!K$G$9!#e(B
ERB::Compilere$B$r2~B$$7$F$bE$i$l$J$$$@$m$&$J$“!”$H4|BT$7$F$^$9!#e(B

e$B$J$s$H!"e(Berb.rb e$B$N@5<0$J;EMM$G$7$?$+!#N;2r$7$^$7$?!#e(B
e$B$G$Oe(B :nodoc: e$B$O<h$j=|$$$?J}$,$$$$$G$9$M!#e(B

e$B$"$He(B Compiler class e$B$Oe(B ERB
e$B$N;EMM$K$O4^$a$J$$$b$N$H$7$Fe(B
e$B9M$($^$9!#e(B

e$B$3$l$C$F!“Hs8x3+$H$7$F$$$ke(B Compiler e$B%*%V%8%'%/%H$,e(B
e$B0z?t$KI,MW$J$s$G$9$h$M!#@_7W$H$7$F$O$”$s$^$j$h$m$7$/e(B
e$B$J$$$H;W$$$^$9$,!"$I$&$G$7$g$&$+!#e(B
e$B!t$=$b$=$be(B set_eoutvar() e$B$OHs8x3+%a%=%C%I!)e(B
e$B!t$J$ie(B :nodoc: e$B$D$1$F$[$7$$$G$9!#e(B
e$B!tCzG+$K$be(B RDoc e$B$,$D$$$F$$$k$+$i!"e(BERB e$B$N;EMM$@$He(B
e$B!t;W$C$F$7$^$$$^$7$?!#e(B

e$B$H$$$&$+!"%*%W%7%g%s0z?t$N$3$H$r$^$C$?$/9MN8$7$F$$$J$$e(B
CGI#initalize() e$B$H$O0c$C$F!"e(BERB#initialize() e$B$O0z?t$Ge(B
eoutvar e$B$r<h$k$h$&$K$A$c$s$H@_7W$5$l$F$$$k$N$G!“e(B
e$B$G$-$l$P$3$l$G:Q$`$h$&$J@_7W$,$$$$$H;W$$$^$9!#e(B
ERB#eoutvar e$B$He(B ERB#eoutvar= e$B$O$”$C$F$b$$$$$H;W$$$^$9$,!#e(B

e$B$?$H$($P$3$s$J$N$O$I$&$G$7$g$&$+!#MW7o$rK~$?$7$F$$$k$+e(B
e$B$I$&$+J,$+$j$^$;$s$,!#e(B

class ERB
def initialize(eruby_str, safe_level=nil, trim_mode=nil,
eoutvar=‘_eoutvar’)

@compiler = Compiler.new
@compiler.eoutvar = eoutvar

end
def eoutvar=(name)
@compiler.eoutvar = name
end
def eoutvar
@compiler.eoutvar
end
end

e$B$"$k$$$Oe(B ERB class e$B$r3HD%$9$k$N$G$O$J$/$F!"e(Bcomposition
e$B$r;H$C$Fe(B
e$B;HMQ$9$ke(B Compiler class e$B$r;XDj$G$-$k$h$&$K$9$k$H$+!#e(B

e$B@>;3OB9-$G$9!#e(B

At Sun, 10 Feb 2008 08:53:24 +0900,
Masatoshi SEKI wrote:

  1. ERB#result() e$B$G!"e(Bsafe_level e$B$,@_Dj$5$l$F$$$k$H$-$Ke(B
    Thread e$B$r@8@.$7$F$+$i$=$NCf$Ge(B eval e$B$7$F$$$^$9$,!"e(B
    Thread e$B$r@8@.$9$kM}M3$r65$($F$$$?$@$1$^$;$s$+!#e(B

e$B$?$7$+!“e(B$SAFEe$B$O$h$jBg$-$JCM$7$+@_Dj$G$-$J$$$+$i$@$C$?$He(B e$B;W$$$^$9!#e(B
e$B8F$S=P$785$Ne(B$SAFEe$B$,JQ$o$C$F$7$^$&$N$rHr$1$k$?$a$G$9$M!#e(B
e$B:#$J$i$b$C$H$&$^$$J}K!$,$”$k$+$b$7$l$^$;$s!#e(B

e$B$$$D$+$J$N$+$OCN$i$J$$$N$G$9$,!"e(BThreade$B$r;H$o$J$/$F$be(B
proce$B$@$1$G0l;~E*$KJQ99$G$-$k$h$&$G$9!#e(B

% ruby -e ‘proc{$SAFE=1; p $SAFE}.call; p $SAFE’
1
0
%

e$B7,ED$G$9!#e(B

2008/2/10 Masatoshi SEKI [email protected]:

???
e$BJL$K9T$r$^$?$$$G$$$F$b!"%(%i!<%a%C%;!<%8$G$Oe(B Ruby e$B$,e(B
e$B9THV9f$r@5$7$/=P$7$F$/$l$k$H;W$&$s$G$9$,!#e(B

e$B$?$H$($P<!$N$h$&$Je(B eRuby e$B%U%!%$%k$,$"$C$?$H$7$F!'e(B

hoge.rhtml

<% for item in @list %> <% end %>
<%= item.upcase %>
--------------------

Erubis e$B$OO"B3$7$?9T$re(B 1 e$B$D$NJ8;zNs$K$7$^$9!#e(B
e$B<!$NNc$@$He(B 3 e$B9TL$+$ie(B 4 e$B9TL$H!"e(B4 e$B9TL$+$ie(B 5
e$B9TL$Ge(B
e$B9T$r$^$?$$$@J8;zNs$,$G$F$-$^$9!#e(B


$ erubis -x hoge.rhtml | cat -n
1 _buf = ‘’; _buf << '
2 ‘; for item in @list
3 _buf << ’
4
5
6 '; end
7 _buf << '

’; _buf << ( item.upcase ).to_s; _buf << '

8 ';
9 _buf.to_s

e$B$G$b$3$N$;$$$G!“%(%i!<$,$”$C$?$H$-$Ke(B Ruby e$B$,$*$+$7$Je(B
e$B9THV9f$rI=<($9$k$h$&$J$3$H$O$J$$$G$9$h$M!#e(B
e$B<!$N$Oe(B @list e$B$Ke(B [‘aaa’, ‘bbb’, nil] e$B$r@_Dj$7$F<B9Te(B
e$B$7$F$$$kNc$G$9$,!“e(B4 e$B9TL$Ke(B <%= item.upcate %>
e$B$,$”$k$N$G!“e(B
nil.upcase e$B$,%(%i!<$K$J$C$F$$$^$9!#e(B
Ruby e$B$O!”%(%i!<$Oe(B 4 e$B9TL$G$"$k$3$H$r$A$c$s$HJs9p$7$Fe(B
e$B$/$l$F$$$k$3$H$,$o$+$j$^$9!#e(B


$ erubis -c ‘{list: [aaa, bbb, null]}’ hoge.rhtml
hoge.rhtml:4: undefined method `upcase’ for nil:NilClass (NoMethodError)

e$B$*$C$7$c$C$F$$$k$N$O!"<0E83+$r;H$C$?>l9g$N$3$H$G$7$g$&$+!#e(B
e$B$=$N>l9g$G$b!"FC$KLdBj$O$J$$$s$G$9$,!#e(B


$ erubis -E Interpolation -x hoge.rhtml | cat -n
1 _buf = ‘’; _buf << %Q<table>\n
2 for item in @list
3 _buf << %Q <tr> 4 <td>#{ item.upcase }</td> 5 </tr>\n
6 end
7 _buf << %Q</table>\n
8 _buf.to_s
$ erubis -E Interpolation -c ‘{list: [aaa, bbb, null]}’ hoge.rhtml
hoge.rhtml:4: undefined method `upcase’ for nil:NilClass (NoMethodError)

e$BJ#?t9T$K$o$?$kJ8;zNs$de(B here document
e$B$NCf$G<0E83+$r;H$C$F$b!"e(B
Ruby e$B$O8-$$$+$i@5$7$$%(%i!<9THV9f$r$@$7$F$/$l$^$9!#e(B

e$B$J$K$+<ALd$N0U?^$r4V0c$C$F$$$?$i$9$_$^$;$s!#e(B

e$B31$H$$$$$^$9!#e(B

On 2008/02/11, at 2:47, Makoto Kuwata wrote:

e$B$=$l$G$O!"e(BThread e$B$r;H$&$+$I$&$+$O;EMM$G$O$J$/!"e(B
e$B!Ve(Bsafe_level e$B$r@_Dj$7$?$H$-$K$=$l$,%a%$%s%W%m%0%i%`$Ne(B
$SAFE e$B$K1F6A$r5Z$$5$J$$$3$H!W$,;EMM$@$H$$$&M}2r$Ge(B
e$B$h$m$7$$$G$7$g$&$+!#e(B

e$B$O$$!#e(B

  • ERB::DefMethod

e$B$J$s$H!"e(Berb.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e$B7,ED$G$9!#e(B

2008/2/11 Masatoshi SEKI [email protected]:

e$B$=$l$G$O!"e(BThread e$B$r;H$&$+$I$&$+$O;EMM$G$O$J$/!"e(B
e$B!Ve(Bsafe_level e$B$r@_Dj$7$?$H$-$K$=$l$,%a%$%s%W%m%0%i%`$Ne(B
$SAFE e$B$K1F6A$r5Z$$5$J$$$3$H!W$,;EMM$@$H$$$&M}2r$Ge(B
e$B$h$m$7$$$G$7$g$&$+!#e(B

e$B$O$$!#e(B

e$BN;2r$G$9!#e(B

e$B$J$s$H!"e(Berb.rb e$B$N@5<0$J;EMM$G$7$?$+!#N;2r$7$^$7$?!#e(B
e$B$G$Oe(B :nodoc: e$B$O<h$j=|$$$?J}$,$$$$$G$9$M!#e(B

rdoce$B$X$N%Q%C%A$OA4$F<h$j9~$`$D$b$j$G$9!#e(B
e$B$<$RE,@Z$Je(Brdoce$B$rAw$C$F$/$@$5$$e(B! > e$B$_$J$5$se(B

e$BAw$j$^$9!#e(B
e$B$D$$$G$K!“e(BERB#filename e$B$NCM$,e(B ERB#def_module e$B$He(B
ERB#def_class e$B$Ke(B
e$BH?1G$5$l$k$h$&$KJQ99$7$F$$$^$9!#e(B
e$B!t%Q%C%A$,2u$l$?$i?=$7Lu$”$j$^$;$s!#e(B

Index: lib/erb.rb

— lib/erb.rb (revision 15382)
+++ lib/erb.rb (working copy)
@@ -744,19 +744,49 @@
end
end

  • def def_method(mod, methodname, fname=‘(ERB)’) # :nodoc:
  • Define methodname as instance method of mod from compiled ruby

source.

  • example:

  • filename = ‘example.rhtml’ # ‘arg1’ and ‘arg2’ are used in

example.rhtml

  • erb = ERB.new(File.read(filename))

  • erb.def_method(MyClass, ‘render(arg1, arg2)’, filename)

  • print MyClass.new.render(‘foo’, 123)

  • def def_method(mod, methodname, fname=‘(ERB)’)
    mod.module_eval(“def #{methodname}\n” + self.src + “\nend\n”,
    fname, 0)
    end
  • def def_module(methodname=‘erb’) # :nodoc:
  • Create unnamed module, define methodname as instance method of

it, and return it.

  • example:

  • filename = ‘example.rhtml’ # ‘arg1’ and ‘arg2’ are used in

example.rhtml

  • erb = ERB.new(File.read(filename))

  • erb.filename = filename

  • MyModule = erb.def_module(‘render(arg1, arg2)’)

  • class MyClass

  • include MyModule

  • end

  • def def_module(methodname=‘erb’)
    mod = Module.new
  • def_method(mod, methodname)
  • def_method(mod, methodname, @filename || ‘(ERB)’)
    mod
    end
  • def def_class(superklass=Object, methodname=‘result’) # :nodoc:
  • Define unnamed class which has methodname as instance method,

and return it.

  • example:

  • class MyClass_

  • def initialize(arg1, arg2)

  • @arg1 = arg1; @arg2 = arg2

  • end

  • end

  • filename = ‘example.rhtml’ # @arg1 and @arg2 are used in

example.rhtml

  • erb = ERB.new(File.read(filename))

  • erb.filename = filename

  • MyClass = erb.def_class(MyClass_, ‘render()’)

  • print MyClass.new(‘foo’, 123).render()

  • def def_class(superklass=Object, methodname=‘result’)
    cls = Class.new(superklass)
  • def_method(cls, methodname)
  • def_method(cls, methodname, @filename || ‘(ERB)’)
    cls
    end
    end
    @@ -810,15 +840,46 @@
    #–

ERB::DefMethod

class ERB

  • module DefMethod # :nodoc:
  • Utility module to define eRuby script as instance method.

  • === Example

  • example.rhtml:

  • <% for item in @items %>

  • <%= item %>

  • <% end %>

  • example.rb:

  • require ‘erb’

  • class MyClass

  • extend ERB::DefMethod

  • def_erb_method(‘render()’, ‘example.rhtml’)

  • def initialize(items)

  • @items = items

  • end

  • end

  • print MyClass.new([10,20,30]).render()

  • result:

  • 10

  • 20

  • 30

  • module DefMethod
    public
  • def def_erb_method(methodname, erb)
  • define methodname as instance method of current module, using

ERB object or eRuby file

  • def def_erb_method(methodname, erb_or_fname)
    if erb.kind_of? String
  • fname = erb
  • File.open(fname) {|f| erb = ERB.new(f.read) }
  • erb.def_method(self, methodname, fname)
  •    fname = erb_or_fname
    
  •    erb = ERB.new(File.read(fname))
    
  •    erb.def_method(self, methodname, fname)
     else
    
  • erb.def_method(self, methodname)
  •    erb = erb_or_fname
    
  •    erb.def_method(self, methodname, erb.filename || '(ERB)')
     end
    
    end
    module_function :def_erb_method
    ===================================================================

e$B$3$l$C$F!“Hs8x3+$H$7$F$$$ke(B Compiler e$B%*%V%8%'%/%H$,e(B
e$B0z?t$KI,MW$J$s$G$9$h$M!#@_7W$H$7$F$O$”$s$^$j$h$m$7$/e(B
e$B$J$$$H;W$$$^$9$,!"$I$&$G$7$g$&$+!#e(B

e$B$=$&$$$&$b$N$J$N$G$9$+!#e(B
compilere$B$KBP$9$k%;%C%H%"%C%W$re(BCompilere$B$K;}$?$;$?$/$J$/$Fe(B
ERBe$B$KG[CV$7$?$s$@$1$I$@$a$G$7$g$&$+!#e(B
privatee$B$K$7$F$/$le(B! e$B$J$iF10U$7$^$9!#e(B

private e$B$K$7$F$b$$$$$N$J$i!“$<$R$=$&$9$k$3$H$r$*4+$a$7$^$9!#e(B
e$B:#$@$He(B RDoc e$B$,$D$$$F$$$k$N$G!”$I$&$_$F$be(B ERB
e$B$N8x<0$J;EMM$He(B
e$B9M$($i$l$^$9!#e(B

#e$B!V5$J,E*$K$OHs8x3+!W$H$$$C$F$b<B:]$K$O8x3+$G$9$h$M!#e(B

e$B5;=QE*$K$O$=$&$G$9$1$I!“%I%-%e%a%s%H$K$^$G@Q6KE*$K5-:$9$ke(B
e$BI,MW$O$J$$$G$9$h$M!#e(B
e$B%3!<%I$d%I%-%e%a%s%H$G!VHs8x3+$G$”$k!W$HL@5-$9$k$@$1$G==J,e(B
e$B$@$H;W$$$^$9!#$=$N$?$a$Ne(B :nodoc: e$B$G$9!#e(B

#Rubye$B$J$N$G9%$-$J$h$&$K<B9T;~$K<j$,F~$l$i$l$^$9!#e(B
#e$B$J$N$G!"$I$3$r$$$8$C$F$bC/$+$,!V$.$c$C!W$H8@$&$G$7$g$&!#e(B

e$B%I%-%e%a%s%H$K5-:$7$F$J$$5!G=$r$$$8$C$F!V$.$c$C!W$H$J$l$P!“e(B
e$B$=$l$O$=$N?M$N>!<j$G$9!#e(B
e$B5-:$7$F$”$k5!G=$r;H$C$F$$$F!V$.$c$C!W$H$J$l$P!"$=$l$Oe(B
e$B3+H/<T$N@UG$$G$9!#e(B

ERBe$B8_49e(Bclasse$B$,=q$-$K$/$$$C$FOC$G$9$+e(B?

e$B$$$$$(!"e(BERB e$B$N;EMM$rL@3N$K$7$?$$$H$$$&OC$G$9!#e(B
e$B:#!"e(BERB e$BMQ$N%F%9%H%9%/%j%W%H$r=q$$$F$$$F!“e(BRDoc
e$B$K:$C$F$$$ke(B
e$B5!G=$rLVMe$7$h$&$H$7$F$$$?$N$G!”<ALd$5$;$F$$$?$@$-$^$7$?!#e(B

e$B;d$Oe(Berubise$B$r%Q%/$C$Fe(BCompilere$B$r2~B$$7$h$&$H;W$C$F$^$9!#e(B

Erubis e$B$O3HD%@-$r=E;k$7$F$o$j$HJ#;($J$3$H$r$7$F$$$k$N$G!"e(B
Compiler e$B$N;29M$K$9$k$J$i!"e(BErubis e$B$K4^$^$l$F$$$ke(B tiny.rb
e$B$re(B
e$B;29M$K$9$k$N$,4JC1$G$9!#e(B
tiny.rb e$B$Oe(B 50 e$B9TDxEY$Ge(B eRuby
e$B$N%/%i%9$r<BAu$7$F$$$^$9!#e(B

e$B!t$=$b$=$be(B set_eoutvar() e$B$OHs8x3+%a%=%C%I!)e(B
e$B!t$J$ie(B :nodoc: e$B$D$1$F$[$7$$$G$9!#e(B
e$B!tCzG+$K$be(B RDoc e$B$,$D$$$F$$$k$+$i!"e(BERB e$B$N;EMM$@$He(B
e$B!t;W$C$F$7$^$$$^$7$?!#e(B

rdoce$B$N%Q%C%A$rJg=8$7$^$9e(B!

e$B3NG’$J$s$G$9$1$I!"e(Bprivate e$B$K$7$F$b$h$$$H$$$&$3$H$J$N$G!"e(B
ERB#set_eoutvar() e$B$O;EMM$K$O4^$a$J$/$F$$$$$s$G$9$h$M!)e(B

Index: lib/erb.rb

— lib/erb.rb (revision 15382)
+++ lib/erb.rb (working copy)
@@ -704,7 +704,7 @@

to just use the constructor though, since calling this method

requires the

setup of an ERB compiler object.

  • def set_eoutvar(compiler, eoutvar = ‘_erbout’)
  • def set_eoutvar(compiler, eoutvar = ‘_erbout’) # :nodoc:
    compiler.put_cmd = “#{eoutvar}.concat”
    compiler.insert_cmd = “#{eoutvar}.concat”

===================================================================

e$B;d$Oe(Beoutvare$B$be(Bsafe_levele$B$b<:GT$@$C$?$H;W$C$F$^$9!#e(B
e$B$b$C$H$&$^$/;H$$J}$rM6F3$9$k$Y$-$G$7$?!#e(B

e$B$=$&$J$s$G$9$+!)8D?ME*$K$O$=$s$J$KLdBj$H$O;W$o$J$$$s$G$9$,!#e(B
e$B<:GT$@$H$7$?$i!"e(BERB#compile e$B$,$J$/$Fe(B ERB#initialize
e$B$G$7$+e(B
e$B%3%s%Q%$%k$G$-$J$$$3$H$@$H;W$$$^$9!#e(B
e$B!t$3$l$C$Fe(B CGI class (cgi.rb) e$B$bF1$8$h$&$J@_7W$J$s$G$9$h$M!#e(B
e$B!te(BCGI#initializee$B$GA4It%Q!<%9$9$k$+$i:$$k!#e(B

Compliere$B$O0l;~E*$J%3%s%F%-%9%H$H$$$&$+=hM}%*%V%8%'%/%H$J$N$Ge(B
eoutver=e$B$G$O<jCY$l$_$?$$$G$9!#e(B

e$B$J$i$d$O$je(B Compiler
e$B%*%V%8%'%/%H$r;XDj$G$-$k$h$&$K@_7W$7$?J}$,e(B
e$B$h$+$C$?$N$+$J$H;W$$$^$9!#e(B
e$B$3$l$@$H!“e(Btrim_mode e$B$He(B set_eoutvar e$B$N5!G=$re(B ERB class
e$B$+$ie(B
Compiler class e$B$XJ,N%$G$-!”$9$C$-$j$7$^$9!#e(B

compiler = Compiler.new(:trim=>‘>’, :eoutvar=>'_erbout)
erb = ERB.new(eruby_src, safe_level, compiler)

e$B$“$”$=$&$+!"$3$l$@$He(B Compiler class
e$B$r8x3+$7$J$$$H$$$1$J$$$N$+!#e(B
e$BFq$7$$$G$9$M!#e(B

e$B$8$c$"$=$b$=$b!"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ne(BERBe$B$N5sF0$KD>$7$F$"$k$s$@$1$I!“e(B
e$B7,ED$5$s$N$r8+$F$+$ie(Bsvne$B$KF~$l$h$&$+$J$”!#e(B

e$B3NG’$J$s$G$9$1$I!"e(Bprivate e$B$K$7$F$b$h$$$H$$$&$3$H$J$N$G!"e(B
ERB#set_eoutvar() e$B$O;EMM$K$O4^$a$J$/$F$$$$$s$G$9$h$M!)e(B

e$B%F%9%H=q$+$J$/$F$b$h$$$+!)$H$$$&$3$H$G$7$?$i!"$h$$$G$9!#e(B
e$BI,MW$K$J$C$?$i<+J,$G=q$-$^$9!#e(B

e$B:9$7;Y$($J$1$l$P65$($F$/$@$5$$!#e(B
e$BA0=hM}!"8e=hM}$rJQ$($?$j$H$+$G$9$+$M!#%a%=%C%IL>0-$$$1$I!#e(B
eoutvare$B$N=i4|CM$re(BStringe$B0J30$K$9$k$3$H$Ge(BHTMLe$B8~$1$Ne(B
e$B<+F0e(Bquotee$BHGe(Berbe$B$r=q$-$^$7$?!#e(B

e$B7,ED$G$9!#e(B

2008/2/12 Masatoshi SEKI [email protected]:

erb-2.0.4.tar.gze$B$Ne(Brunite$B$re(Bruby/teste$B$K%$%s%]!<%H$7e(B
e$B$F$J$$$3$H$Ke(B
e$B@hF|5$IU$-$^$7$?!#8=:_$Ne(BERBe$B$N5sF0$KD>$7$F$"$k$s$@$1$I!“e(B
e$B7,ED$5$s$N$r8+$F$+$ie(Bsvne$B$KF~$l$h$&$+$J$”!#e(B

e$B$3$A$i$G=q$$$?$N$Oe(B for Rubinius e$B$J$s$G!“e(BTest::Unit
e$B$G$O$J$/$Fe(B
RSpec (e$B$Ne(B subset) e$B$J$s$G$9$1$I!”$=$l$G$b$$$$$G$9$+!)e(B
e$B$b$7$44uK>$G$"$l$P%a!<%k$G$*Aw$j$7$^$9!#e(B

e$B3NG’$J$s$G$9$1$I!"e(Bprivate e$B$K$7$F$b$h$$$H$$$&$3$H$J$N$G!"e(B
ERB#set_eoutvar() e$B$O;EMM$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