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

What is a predicate in the context of Ruby method naming conventions?

Câu trả lời

In the context of Ruby method naming conventions, a predicate is a method that returns a boolean value, indicating whether a certain condition is true or false. These methods are easily identifiable by the question mark ? at the end of their names. The use of a question mark is a Ruby-specific convention that signals to the reader that the method will return a boolean-like value, which can be either true, false, or a truthy or falsy value in Ruby terms[3][4].

Predicate methods are intended to be simple queries about the state of an object and should not produce side effects. This means that calling a predicate method should not alter the state of the object or have any other side effects; it should only return information about the object's state[3][4].

For example, consider the following Ruby methods:

ruby Copy
def empty?
  # returns true if a collection is empty
end

def valid?
  # returns true if an object meets certain validation criteria
end

In these examples, empty? and valid? are predicate methods...

junior

junior

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

senior

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

entry

Why Ruby is known as a language of flexibility?

senior

What happens if a block is passed two arguments but only accepts one argument?

Bình luận

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

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