Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
ERB#def_method
(::Module, ::String, ?::String) -> untyped
def_method(module, method_signature, filename = '(ERB)') → method_name
lib/erb.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/lib/erb.rb, line 1146
def def_method(mod, methodname, fname='(ERB)')
unless initialized_by_new?
raise ArgumentError, "not initialized"
end
src = self.src.sub(/^(?!#|$)/) {"def #{methodname}\n"} << "\nend\n"
mod.module_eval do
eval(src, binding, fname, -1)
end
end
Creates and returns a new instance method in the given module module; returns the method name as a symbol.
The method is created from the given method_signature, which consists of the method name and its argument names (if any).
The filename sets the value of filename; see Error Reporting.
Example
template = '<%= arg1 %> <%= arg2 %>' erb = ERB.new(template) MyModule = Module.new erb.def_method(MyModule, 'render(arg1, arg2)') # => :render class MyClass; include MyModule; end MyClass.new.render('foo', 123) # => "foo 123"