Is a method an object?
Is a method an object?
In Ruby, methods themselves are not objects, but they can be wrapped in objects called Method objects. This can be done by calling the Object#method method, passing in the symbol of the method's name. This allows you to store the method in a variable, pass it around as an argument, or call it later. Here's an example:
class MyClass
def my_method
"Hello, World!"
end
end
obj = MyClass.new
method_object = obj.method(:my_method)
puts method_object.call
# Output: "Hello, World!"
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.