Module: ERB::DefMethod

Defined in:
lib/erb.rb

Overview

Utility module to define eRuby script as instance method.

Example

example.rhtml:

<% for item in @items %>
<b><%= item %></b>
<% 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:

<b>10</b>

<b>20</b>

<b>30</b>

Class Method Summary collapse

Class Method Details

.def_erb_method(methodname, erb_or_fname) ⇒ Object

define methodname as instance method of current module, using ERB object or eRuby file



1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'lib/erb.rb', line 1011

def def_erb_method(methodname, erb_or_fname)
  if erb_or_fname.kind_of? String
    fname = erb_or_fname
    erb = ERB.new(File.read(fname))
    erb.def_method(self, methodname, fname)
  else
    erb = erb_or_fname
    erb.def_method(self, methodname, erb.filename || '(ERB)')
  end
end