1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-16 21:48:02 +00:00

Compare commits

...

3 Commits

Author SHA1 Message Date
nick-w-nick
e2f0cfe471 Merge 295fe153ad into 96c394c198 2025-02-03 16:51:40 -05:00
jbranchaud
96c394c198 Add Extract Capture Group Matches With String Slices as a Ruby TIL 2025-02-03 15:29:56 -06:00
nick-w-nick
295fe153ad added mention of ES6 compatibility
Hello, I've added a small blockquote below the description to indicate that this method of accessing an indefinite number of function arguments has been superseded by the use of the spread operator via rest parameters for ES6+ compatibility.
2022-01-06 11:39:04 -05:00
3 changed files with 41 additions and 1 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).
_1583 TILs and counting..._
_1584 TILs and counting..._
See some of the other learning resources I work on:
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
@@ -1279,6 +1279,7 @@ See some of the other learning resources I work on:
- [Exit A Process With An Error Message](ruby/exit-a-process-with-an-error-message.md)
- [Expect A Method To Be Called And Actually Call It](ruby/expect-a-method-to-be-called-and-actually-call-it.md)
- [Extract A Column Of Data From A CSV File](ruby/extract-a-column-of-data-from-a-csv-file.md)
- [Extract Capture Group Matches With String Slices](ruby/extract-capture-group-matches-with-string-slices.md)
- [FactoryGirl Sequences](ruby/factory-girl-sequences.md)
- [Fail](ruby/fail.md)
- [Fetch Warns About Superseding Block Argument](ruby/fetch-warns-about-superseding-block-argument.md)

View File

@@ -5,6 +5,8 @@ an array-like object with all of the arguments to the function. Even if not
all of the arguments are referenced in the function signature, they can
still be accessed via the `arguments` object.
> For ES6+ compatibility, the `spread` operator used via [rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) is preferred over the `arugments` object when accessing an abritrary number of function arguments.
```javascript
function argTest(one) {
console.log(one);

View File

@@ -0,0 +1,37 @@
# Extract Capture Group Matches With String Slices
Ruby's _string slice_ syntax allows us to use the square brackets to access
portions of a string. It's most common to pass positional integer index
arguments or a range. However, in true Ruby fashion, another way of thinking
about defining the slice of a string is based on a regex match.
We can pass a regex and an int (specifying which match we want) to extract some
portion of a string based on the regex match. That includes capture groups.
Here are a couple examples of extracting matching capture groups as well as
getting the entire regex match:
```ruby
> "me+abc123@email.com"[/.+\+(.+)@(.+)/, 1]
=> "abc123"
> "me+abc123@email.com"[/.+\+(.+)@(.+)/, 2]
=> "email.com"
> "me+abc123@email.com"[/.+\+(.+)@(.+)/, 0]
=> "me+abc123@email.com"
> "me+abc123@email.com"[/.+\+(.+)@(.+)/]
=> "me+abc123@email.com"
```
The `0`th match (which is the default) corresponds to the full match. Each
integer position after that corresponds to any capture groups. This maps
directly to the underlying `MatchData` object:
```ruby
> /.+\+(.+)@(.+)/.match("me+abc123@email.com")
=> #<MatchData "me+abc123@email.com" 1:"abc123" 2:"email.com">
```
[source](https://ruby-doc.org/3.3.6/String.html#class-String-label-String+Slices)