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')
lib/erb.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/lib/erb.rb, line 500
def def_class(superklass=Object, methodname='result')
cls = Class.new(superklass)
def_method(cls, methodname, @filename || '(ERB)')
cls
end
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()