Actions
Feature #22486
openAnonymous method
Feature #22486:
Anonymous method
Status:
Open
Assignee:
-
Target version:
-
Description
I propose an API Method.new(receiver, name = nil, &block) to define an anonymous singleton method and UnboundMethod.new(owner, name = nil, &block) to define an anonymous instance method. The default value of name can be :anonymous for compatibility concern (existing code bases may have been assuming Method#name is not nil). This functionality can currently be achieved in pure Ruby as follows:
def Method.new receiver, name = :anonymous, &body
old = begin
receiver.singleton_method name
rescue NameError
nil
end
receiver.define_singleton_method name, &body
result = receiver.method name
if old
receiver.define_singleton_method name, old
else
receiver.singleton_class.undef_method name
end
result
end
def UnboundMethod.new owner, name = :anonymous, &body
old = begin
owner.instance_method name
rescue NameError
nil
end
owner.define_method name, &body
result = owner.instance_method name
if old
owner.define_method name, old
else
owner.undef_method name
end
result
end
A use case is when one wants to pass a block with a block argument to instance_exec. For example:
No data to display
Actions