Skip to content

Commit

Permalink
Add guideline about omitting super arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Earlopain committed May 22, 2024
1 parent 89ed244 commit 295c922
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3216,6 +3216,42 @@ def some_method(&)
end
----

=== Super Forwarding

Use `super` without arguments when the enclosing methods arguments are the same.

When super is called without arguments and parentheses Ruby will automatically forward the arguments taken by the method.

[source,ruby]
----
# bad
def some_method(*args, **kwargs)
super(*args, **kwargs)
end
# good - implicitly passing all arguments
def method(*args, **kwargs)
super
end
# good - forwarding a subset of the arguments
def method(*args, **kwargs)
super(*args)
end
# good - calling super with different arguments
def method(*args, **kwargs)
super("foo", *args, **kwards)
end
# good - forwarding no arguments
def method(*args, **kwargs)
super()
end
----

IMPORTANT: Blocks are always passed to the parent method. `&nil` can be passed to `super` if the block should remain local.

=== Private Global Methods [[private-global-methods]]

If you really need "global" methods, add them to Kernel and make them private.
Expand Down

0 comments on commit 295c922

Please sign in to comment.