Câu hỏi phỏng vấn Ruby
Câu hỏi

Is a method...

Câu trả lời

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:

ruby Copy
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

senior

Gợi ý câu hỏi phỏng vấn

junior

Is Ruby a strongly typed or a weakly typed language?

junior

Are instance methods public or private?

middle

Why might you want to avoid using string literals within loops?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào