“super vs super () ruby” Kode Jawaban

super vs super () ruby

# super program
class Parent
  def say(message)
    p message
  end
end

class Child < Parent
  def say(message)
    super
  end
end

Child.new.say('Hi Rubyist!') # => "Hi Rubyist!"


# super() program
class Parent
  def say
    p "I'm the parent"
  end
end

class Child < Parent
  def say(message)
    super()
  end
end

Child.new.say('Hi!') # => "I'm the parent"

# super with block
class Parent
  def say
    yield
  end
end

class Child < Parent
  def say
    super
  end
end

Child.new.say { p 'Hi! Glad to know you Parent!' } # => "Hi! Glad to know you Parent!"
MunnaBhaiyya

super vs super () ruby

super - sends all arguments passed to the function to parent
super() - no arguments
MunnaBhaiyya

Jawaban yang mirip dengan “super vs super () ruby”

Pertanyaan yang mirip dengan “super vs super () ruby”

Lebih banyak jawaban terkait untuk “super vs super () ruby” di Ruby

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya