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

What is the difference between Module#remove_method and Module#undef_method ?

Câu trả lời

The difference between Module#remove_method and Module#undef_method in Ruby lies in their behavior towards method resolution and inheritance.

Module#remove_method is used to remove a method from a module or a class. When you call remove_method, it removes the definition of the method from the receiver, which is the class or module from which you are removing it. If the method is defined in a superclass or included module, the method from the ancestor will be called instead, as the method lookup proceeds up the inheritance chain[1].

On the other hand, Module#undef_method is used to prevent a method from being called, even if it is defined in a superclass or included module. When you call undef_method, it undefines the method in such a way that any calls to that method will result in a NoMethodError, regardless of whether the method is defined in an ancestor or not. This is because undef_method actively preve...

middle

middle

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

senior

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

junior

What is a DSL and how does it pertain to Ruby?

junior

What is duck typing and how does it pertain to Ruby?

Bình luận

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

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