File: custom-predicates.html.md

package info (click to toggle)
ruby-dry-logic 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 728 kB
  • sloc: ruby: 4,929; makefile: 6
file content (33 lines) | stat: -rw-r--r-- 648 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
---
title: Custom predicates
layout: gem-single
name: dry-logic
---

Define custom predicates using the `predicate` method. Use the build in builder to evaluate and define your predicate.

``` ruby
extend Dry::Logic::Builder

build do
  predicate :divisible_with? do |num, input|
    (input % num).zero?
  end
end
```

Then create your predicate

``` ruby
is_divisible_with_ten = build do
  divisible_with?(10)
end
```

Here, `10` represents the first argument `num` to `divisible_with?`.

``` ruby
# 10 & 5 is passed as {input} to your method
is_divisible_with_ten.call(10).success? # => true
is_divisible_with_ten.call(5).success? # => false
```