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

Does Ruby Support Single Inherita...

Câu trả lời

Ruby supports single inheritance, meaning each class can have only one parent class[1][3][4]. However, Ruby provides a way to simulate multiple inheritance using modules (also known as mixins)[1][2][3].

With single inheritance, a class can inherit methods and properties from one parent class. For example:

ruby Copy
class Parent
  def parent_method
    puts "This is a parent method."
  end
end

class Child < Parent
end

child = Child.new
child.parent_method  # Output: This is a parent method.

In this case, Child inherits from Parent[4].

To achieve something similar to multiple inheritance, Ruby uses modules. A class can include multiple modules, allowing it to access methods defined in those modules[1][2][3]. This is known as a mixin. For example:

ruby Copy
module A
  def a_method
    puts "This is a method from module A."
  end
end

module B
  def b_metho...
senior

senior

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

senior

What is the difference between content_for and yield ?

middle

What Is The Difference Between Delete And Destroy ?

senior

What is the Difference Between Gem And Plugin?

Bình luận

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

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