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

How Is Visibility Of Methods Changed In Ruby (encapsulation)?

Câu trả lời

In Ruby, you can control the visibility of methods using the keywords public, private, and protected. This allows you to achieve encapsulation by hiding implementation details and controlling access to the methods of a class[1].

Public Methods

Public methods are the default visibility in Ruby. They can be called from anywhere, both inside and outside the class[1].

Private Methods

Private methods can only be called from within the class they are defined in, or from subclasses. They are implicitly invoked on self and cannot be called with an explicit receiver[1][2].

To define a private method, use the private keyword followed by the method definitions:

ruby Copy
class MyClass
  def public_method
    # ...
  end

  private

  def private_method
    # ...
  end
end

Protected Methods

Protected methods are similar to private methods, but they can be call...

junior

junior

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

middle

What Is The Difference Between Delete And Destroy ?

middle

Explain what is Interpolation in Ruby?

junior

How Many Types Of Relationships Does A Model Has?

Bình luận

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

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