1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-15 04:58:02 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Bob Conan
13ddbd5dca Merge 5615da920f into 7f1c243310 2024-11-23 11:14:46 -05:00
jbranchaud
7f1c243310 Add Gather Positional Arguments In Method Definition as a Ruby TIL 2024-11-23 10:11:49 -06:00
Bob Conan
5615da920f Update README.md, fix typos 2024-11-15 16:16:31 -06:00
BobConanDev
c60c63f554 Updated README.md, fix typo(s) 2024-11-15 16:42:57 -05:00
2 changed files with 59 additions and 3 deletions

View File

@@ -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).
_1514 TILs and counting..._
_1515 TILs and counting..._
---
@@ -190,7 +190,7 @@ _1514 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 @@ _1514 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)
@@ -1225,6 +1225,7 @@ _1514 TILs and counting..._
- [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)
- [Gather Positional Arguments In Method Definition](ruby/gather-positional-arguments-in-method-definition.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)

View File

@@ -0,0 +1,55 @@
# Gather Positional Arguments In Method Definition
The `*` symbol can be used in Ruby in a method definition to gather up an
arbitrary number of positional arguments.
For instance, we can gather all positional arguments with this method
definition:
```ruby
def gather_all(*args)
puts args
end
```
Or we can isolate the first positional arg and then gather the rest:
```ruby
def first_and_rest(first, *rest)
puts "First: #{first}, Rest: #{rest}"
end
```
We can even do something a bit more interesting like isolating the first and
last arguments while gathering up everything else in the middle:
```ruby
def pop_parens(left, *middle, right)
if left != '(' || right != ')'
raise "Uh oh!"
else
if middle.size == 1
puts "Found: #{middle.first}"
else
pop_parens(*middle)
end
end
end
```
Here is what it looks like if we splat some different sets of arguments into
that method call:
```ruby
> tokens1 = "((((4))))".split('')
=> ["(", "(", "(", "(", "4", ")", ")", ")", ")"]
> tokens2 = "((4))))".split('')
=> ["(", "(", "4", ")", ")", ")", ")"]
> pop_parens(*tokens1)
Found: 4
=> nil
> pop_parens(*tokens2)
(irb):87:in `pop_parens': Uh oh! (RuntimeError)
```
[source](https://ruby-doc.org/3.3.6/syntax/methods_rdoc.html#label-Array-2FHash+Argument)