“super vs super () ruby” Réponses codées

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

Réponses similaires à “super vs super () ruby”

Questions similaires à “super vs super () ruby”

Plus de réponses similaires à “super vs super () ruby” dans Ruby

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code