Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Ruby 2.7+ Pattern Matching #25

Merged
merged 1 commit into from
Apr 25, 2024

Conversation

danielma
Copy link
Contributor

Add #deconstruct_keys to Expression::Base and Schedule so that they can be used in pattern matches.

In our products, we've been using #to_h for this, and it would be nice sugar to have it built in.

A common example we've used in our products is building a humanized-readable version of a schedule. An extremely stripped down version might look something like this

case existing_user_schedule
in weekday: { weekday: }
  "Every #{weekday}"
in day_in_month: { day: }
  "Last #{day} of every month"
end

Copy link
Owner

@molawson molawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love how remarkably simple the implementation is here. And I think the use case makes perfect sense!

In fact, I've had an open issue for... a while 😬 ... to build this sort of human readable representation of a schedule.

Would you mind adding an example to the readme?

Add `#deconstruct_keys` to `Expression::Base` and `Schedule` so that
they can be used in pattern matches.

In our products, we've been using `#to_h` for this, and it would be
nice sugar to have it built in.

A common example we've used in our products is building a
humanized-readable version of a schedule. An extremely stripped down
version might look something like this

```ruby
case existing_user_schedule
in weekday: { weekday: }
  "Every #{weekday}"
in day_in_month: { day: }
  "Last #{day} of every month"
end
```
@danielma danielma force-pushed the dma/schedule-pattern-match branch from fb1d64e to 3e595c4 Compare April 23, 2024 18:04
@danielma
Copy link
Contributor Author

Added an example to the readme!

@molawson molawson merged commit fcc1fc0 into molawson:main Apr 25, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants