Extended maintenance of Ruby 1.9.3 ended on February 23, 2015. Read more

In Files

  • erb.rb

Class/Module Index [+]

Quicksearch

ERB::DefMethod

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>

Public Class Methods

def_erb_method(methodname, erb_or_fname) click to toggle source

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

 
               # File erb.rb, line 976
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
            

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.