Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

ERB#def_class

(?::Class, ?::String) -> ::Class
def_class(superklass=Object, methodname='result')

Define unnamed class which has methodname as instance method, and return it.

example:

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()
See also .version #def_method .new #def_module also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0