Is a method...
Is a method...
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!"
In the example above, my_method
is a method defined in MyClass
. We create an instance of MyClass
named obj
, and then we get a Method
object by calling obj.method(:my_method)
. We store this Method
object in the method_object
variable, which we can then call later using method_object.call
, producing the output "Hello, World!".
This feature of Ruby allows for gr...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào