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

Is a block ...

Câu trả lời

In Ruby, a block is not an object by itself; it is a piece of code that can be passed to methods and executed within the context of that method. Blocks are one of the foundational features of Ruby, allowing for powerful and flexible code patterns, particularly in iterative operations and callbacks[1].

However, while blocks themselves are not objects, they can be converted into objects of the class Proc. This is done by calling the lambda or Proc.new methods, which transform the block into a Proc object. Once a block has been converted into a Proc object, it can be stored in variables, passed as arguments, and called in different contexts, much like any other object in Ruby[1][2].

Here's a brief example to illustrate this:

ruby Copy
# Define a block and convert it to a Proc object
my_proc = Proc.new { |x| pu...
senior

senior

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

senior

What is the difference between BasicObject#instance_eval and BasicObject#instance_exec ?

junior

What does it mean to coerce an object? Why would you do it?

middle

Why can you safely use a string as a hash key, even though a string is mutable?

Bình luận

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

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