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

Mention what is the difference between calling super() and super call ?

Câu trả lời

Certainly! Here's a detailed answer to the interview question:

Difference Between super() and super in Ruby on Rails

In Ruby, the super keyword is used to call a method with the same name in the superclass. However, there are subtle differences between super() and super:

super

When you use super without parentheses, it calls the method in the superclass with the same name and passes all the arguments that were passed to the current method. This is useful when you want to extend the functionality of a method while preserving the original arguments.

Example:

ruby Copy
class Parent
  def greet(name)
    "Hello, #{name}!"
  end
end

class Child < Parent
  def greet(name)
    super + " How are you?"
  end
end

child = Child.new
puts child.greet("Alice")  # Output: "Hello, Alice! How are you?"

In this example, super calls the greet method in the Parent class with the argument name.

super()

When you use super() with parentheses but without any arguments i...

middle

middle

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

middle

Can you explain the difference between ActiveSupport’s HashWithIndifferentAccess and Ruby’s Hash ?

senior

What is Rack?

senior

What are Strong Parameters?

Bình luận

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

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