mirror of
https://github.com/jbranchaud/til
synced 2026-01-16 13:38:02 +00:00
Compare commits
4 Commits
314569e65f
...
f99b5b4ad3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f99b5b4ad3 | ||
|
|
543a82730d | ||
|
|
5615da920f | ||
|
|
c60c63f554 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1508 TILs and counting..._
|
||||
_1509 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -190,7 +190,7 @@ _1508 TILs and counting..._
|
||||
- [Aliasing An Ansible Host](devops/aliasing-an-ansible-host.md)
|
||||
- [Allow Cross-Origin Requests To Include Cookies](devops/allow-cross-origin-requests-to-include-cookies.md)
|
||||
- [Allow HTTPS Through Your UFW Firewall](devops/allow-https-through-your-ufw-firewall.md)
|
||||
- [Check For Cached Site Assocation File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
||||
- [Check For Cached Site Association File For iOS](devops/check-for-cached-site-association-file-for-ios.md)
|
||||
- [Check The Status of All Services](devops/check-the-status-of-all-services.md)
|
||||
- [Check The Syntax Of nginx Files](devops/check-the-syntax-of-nginx-files.md)
|
||||
- [Connect To An RDS PostgreSQL Database](devops/connect-to-an-rds-postgresql-database.md)
|
||||
@@ -723,7 +723,7 @@ _1508 TILs and counting..._
|
||||
- [Check If Clusters Are Upgrade Compatible](postgres/check-if-clusters-are-upgrade-compatible.md)
|
||||
- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md)
|
||||
- [Check If User Role Exists For Database](postgres/check-if-user-role-exists-for-database.md)
|
||||
- [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md)
|
||||
- [Check Table For Any Orphaned Records](postgres/check-table-for-any-orphaned-records.md)
|
||||
- [Checking Inequality](postgres/checking-inequality.md)
|
||||
- [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md)
|
||||
- [Clear The Screen In psql](postgres/clear-the-screen-in-psql.md)
|
||||
@@ -1221,6 +1221,7 @@ _1508 TILs and counting..._
|
||||
- [Find The Min And Max With A Single Call](ruby/find-the-min-and-max-with-a-single-call.md)
|
||||
- [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md)
|
||||
- [Format A Hash Into A String Template](ruby/format-a-hash-into-a-string-template.md)
|
||||
- [Forward All Arguments To Another Method](ruby/forward-all-arguments-to-another-method.md)
|
||||
- [Generate A Signed JWT Token](ruby/generate-a-signed-jwt-token.md)
|
||||
- [Generate Ruby Version And Gemset Files With RVM](ruby/generate-ruby-version-and-gemset-files-with-rvm.md)
|
||||
- [Get Info About Your RubyGems Environment](ruby/get-info-about-your-ruby-gems-environment.md)
|
||||
|
||||
45
ruby/forward-all-arguments-to-another-method.md
Normal file
45
ruby/forward-all-arguments-to-another-method.md
Normal file
@@ -0,0 +1,45 @@
|
||||
# Forward All Arguments To Another Method
|
||||
|
||||
There are three types of arguments that a Ruby method can receive. Positional
|
||||
arguments, keyword arguments, and a block argument.
|
||||
|
||||
A method that deals with all three might be defined like this:
|
||||
|
||||
```ruby
|
||||
def forwarding_method(*args, **kwargs, &block)
|
||||
# implementation
|
||||
end
|
||||
```
|
||||
|
||||
Now lets say we have some concrete method that we want to forward these
|
||||
arguments to:
|
||||
|
||||
```ruby
|
||||
def concrete_method(*args, **kwargs)
|
||||
x = args.first || 1
|
||||
key, y = kwargs.first || [:a, 2]
|
||||
|
||||
puts "Dealing with #{x} and key #{key}: #{y}"
|
||||
|
||||
yield(x, y)
|
||||
end
|
||||
```
|
||||
|
||||
We could forward arguments the longhand way like this:
|
||||
|
||||
```ruby
|
||||
def forwarding_method(*args, **kwargs, &block)
|
||||
concrete_method(*args, **kwargs, &block)
|
||||
end
|
||||
```
|
||||
|
||||
However, since Ruby 2.7 we have access to a shorthand "triple-dot" syntax for
|
||||
forwarding all arguments.
|
||||
|
||||
```ruby
|
||||
def forwarding_method(...)
|
||||
concrete_method(...)
|
||||
end
|
||||
```
|
||||
|
||||
[source](https://ruby-doc.org/3.3.6/syntax/methods_rdoc.html#label-Argument+Forwarding)
|
||||
Reference in New Issue
Block a user