Operators
Operators are used in predicate conditions to compare values and evaluate expressions.
Overview
Ruleur provides a comprehensive set of operators for:
- Equality and identity checks
- Numeric comparisons
- Collection operations
- Pattern matching
- Type checking
Comparison Operators
equals
Checks equality using ==.
user(:role).eq?('admin')
order(:status).eq?('pending')not_equals
Checks inequality using !=.
user(:role).not_eq?('guest')
order(:status).not_eq?('cancelled')identical
Checks identity using eql?.
value(:type).identical(expected_type)Numeric Operators
greater_than
Greater than comparison using >.
order(:total).gt?(100)
user(:age).gt?(18)greater_than_or_equal
Greater than or equal comparison using >=.
order(:total).gte?(50)
user(:age).gte?(21)less_than
Less than comparison using <.
order(:total).lt?(1000)
user(:age).lt?(65)less_than_or_equal
Less than or equal comparison using <=.
order(:items_count).lte?(10)
user(:attempts).lte?(3)Collection Operators
contains
Checks if collection includes a value using include?.
user(:roles).contains?('admin')
order(:tags).contains?('express')include?
Checks if value is in a collection.
user(:status).include?(%w[active pending trial])
order(:type).include?(allowed_types)Pattern Matching
matches
Pattern matching using match? or ===.
email(:address).matches(/^[\w+\-.]+@[a-z\d-]+(\.[a-z\d-]+)*\.[a-z]+$/i)
user(:role).matches(/admin|super/)Boolean Operators
truthy
Checks if value is truthy (not nil or false).
user(:admin?) # Implicit truthy check
flag(:enabled).truthyfalsy
Checks if value is falsy (nil or false).
user(:banned?).falsy
flag(:disabled).falseyType Checking
is_a / instance_of
Type checking using is_a?.
record(:object).is_a(User)
value(:data).instance_of(Hash)nil / null
Checks if value is nil.
user(:deleted_at).nil
record(:parent).nullpresent
Checks if value is present (not nil and not empty).
user(:email).present
order(:items).presentblank
Checks if value is blank (nil, false, empty, or whitespace).
user(:notes).blank
order(:special_instructions).blankMethod Call Operator
call
Calls a method with arguments.
# Check if user owns the record
user(:owns?, record)
# Call with multiple arguments
calculator(:compute, value1, value2, operator: :add)Operator Usage
In DSL
rule 'example' do
conditions do
all?(
order(:total).gt?(100),
user(:email).matches(/@company\.com$/),
user(:roles).includes('premium')
)
end
actions do
set :qualified, true
end
endIn YAML
conditions:
type: all
children:
- type: pred
op: greater_than
left:
type: call
recv: { type: ref, root: order }
method: total
right:
type: literal
value: 100Custom Operators
TODO
Document how to create custom operators and extend the operator system.
Operator Precedence
When combining operators, use explicit grouping with all?() and any?():
# Clear precedence with grouping
conditions do
all?(
any?(
user(:admin?),
user(:moderator?)
),
record(:published?)
)
endExamples
Numeric Range Check
rule 'medium_order' do
conditions do
all?(
order(:total).gte?(50),
order(:total).lt?(200)
)
end
actions do
set :tier, 'medium'
end
endEmail Validation
rule 'valid_email' do
conditions do
all?(
user(:email).present,
user(:email).matches(/\A[\w+\-.]+@[a-z\d-]+(\.[a-z\d-]+)*\.[a-z]+\z/i)
)
end
actions do
set :email_valid, true
end
endRole-Based Access
rule 'can_approve' do
conditions do
all?(
user(:roles).includes('approver'),
document(:status).in(%w[pending submitted]),
not?(document(:approved_at).present)
)
end
actions do
allow! :approve
end
endType and Presence Checks
rule 'process_data' do
conditions do
all?(
input(:data).present,
input(:data).is_a(Hash),
input(:data, :items).is_a(Array),
not?(input(:data, :items).blank)
)
end
actions do
set :ready_to_process, true
end
endSee Also
- Condition - Condition types
- Operators Guide - Operator usage patterns
- DSL - DSL syntax reference