Calling super without arguments
In object-oriented languages, it's common to call a method on the parent class from a child class, and just like in many other languages, in Ruby we do so by invoking super.
However, Ruby has a neat behaviour whereby calling super without any arguments will automatically pass all the arguments that were given to the current method:
class Parent
def greet(name, loudly: false)
greeting = "Hello, #{name}!"
loudly ? greeting.upcase : greeting
end
end
class Child < Parent
def greet(name, loudly: true)
super
end
end
Parent.new.greet("Alice")
# => "Hello, Alice!"
Child.new.greet("Alice")
# => "HELLO, ALICE!"
class Parent {
greet(name, loudly = false) {
const greeting = `Hello, ${name}!`;
return loudly ? greeting.toUpperCase() : greeting;
}
}
class Child extends Parent {
greet(name, loudly = true) {
return super.greet(name, loudly);
}
}
new Parent().greet("Alice");
// => "Hello, Alice!"
new Child().greet("Alice");
// => "HELLO, ALICE!"
This is the sort of tiny attention to detail that may appear insignificant in isolation, but adds up within the Ruby language to make it such a joy to work with.
History
This behaviour of naked super has been present in Ruby since its inception.
No other language predating Ruby had exactly this behaviour and syntax. However, CLOS (the Common Lisp Object System) had a similar concept of call-next-method which allowed calling the next method in the method resolution order without having to specify the arguments. Who knows, it might have been a source of inspiration.
The only language I'm aware of post-Ruby that has adopted this behaviour is Crystal, which is syntactically more or less a copy of Ruby.