instance method
ERB#def_class
(?::Class, ?::String) -> ::Class
def_class(super_class = Object, method_name = 'result') → new_class
lib/erb.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/erb.rb, line 1169
def def_class(superklass=Object, methodname='result')
cls = Class.new(superklass)
def_method(cls, methodname, @filename || '(ERB)')
cls
end
Returns a new nameless class whose superclass is super_class, and which has instance method method_name.
Create a template from HTML that has embedded expression tags that use @arg1 and @arg2:
Example
html = <<TEMPLATE <html> <body> <p><%= @arg1 %></p> <p><%= @arg2 %></p> </body> </html> TEMPLATE template = ERB.new(html)
Create a base class that has @arg1 and @arg2:
Example
class MyBaseClass def initialize(arg1, arg2) @arg1 = arg1 @arg2 = arg2 end end
Use method def_class to create a subclass that has method :render:
Example
MySubClass = template.def_class(MyBaseClass, :render)
Generate the result:
puts MySubClass.new('foo', 123).render
<html>
<body>
<p>foo</p>
<p>123</p>
</body>
</html>