Skip to content

Commit 6cff9b8

Browse files
BurdetteLamarmatzbot
authored andcommitted
[ruby/erb] [DOC] Doc for ERB#def_class
ruby/erb@4ca7784f10
1 parent 3861e50 commit 6cff9b8

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

lib/erb.rb

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,19 +1125,58 @@ def def_module(methodname='erb')
11251125
mod
11261126
end
11271127

1128-
# Define unnamed class which has _methodname_ as instance method, and return it.
1128+
# :markup: markdown
11291129
#
1130-
# example:
1131-
# class MyClass_
1132-
# def initialize(arg1, arg2)
1133-
# @arg1 = arg1; @arg2 = arg2
1134-
# end
1130+
# :call-seq:
1131+
# def_class(super_class = Object, method_name = 'result') -> new_class
1132+
#
1133+
# Returns a new nameless class whose superclass is `super_class`,
1134+
# and which has instance method `method_name`.
1135+
#
1136+
# Create a template from HTML that has embedded expression tags that use `@arg1` and `@arg2`:
1137+
#
1138+
# ```
1139+
# html = <<EOT
1140+
# <html>
1141+
# <body>
1142+
# <p><%= @arg1 %></p>
1143+
# <p><%= @arg2 %></p>
1144+
# </body>
1145+
# </html>
1146+
# EOT
1147+
# template = ERB.new(html)
1148+
# ```
1149+
#
1150+
# Create a base class that has `@arg1` and `arg2`:
1151+
#
1152+
# ```
1153+
# class MyBaseClass
1154+
# def initialize(arg1, arg2)
1155+
# @arg1 = arg1;
1156+
# @arg2 = arg2
11351157
# end
1136-
# filename = 'example.rhtml' # @arg1 and @arg2 are used in example.rhtml
1137-
# erb = ERB.new(File.read(filename))
1138-
# erb.filename = filename
1139-
# MyClass = erb.def_class(MyClass_, 'render()')
1140-
# print MyClass.new('foo', 123).render()
1158+
# end
1159+
# ```
1160+
#
1161+
# Use method #def_class to create a subclass that has method `:render`:
1162+
#
1163+
# ```
1164+
# MySubClass = template.def_class(MyBaseClass, :render)
1165+
# ```
1166+
#
1167+
# Generate the result:
1168+
#
1169+
# ```
1170+
# puts MySubClass.new('foo', 123).render
1171+
# <html>
1172+
# <body>
1173+
# <p>foo</p>
1174+
# <p>123</p>
1175+
# </body>
1176+
# </html>
1177+
# ```
1178+
#
1179+
#
11411180
def def_class(superklass=Object, methodname='result')
11421181
cls = Class.new(superklass)
11431182
def_method(cls, methodname, @filename || '(ERB)')

0 commit comments

Comments
 (0)