Class: Ruby::Rego::SetValue

Inherits:
Value
  • Object
show all
Defined in:
lib/ruby/rego/value.rb

Overview

Represents a set value.

Constant Summary collapse

TYPE_NAME =
"set"

Instance Attribute Summary

Attributes inherited from Value

#value

Instance Method Summary collapse

Methods inherited from Value

#==, #fetch_reference, from_ruby, #hash, #object_key, #truthy?, #type_name, #undefined?

Constructor Details

#initialize(elements) ⇒ SetValue

Create a set value.

Parameters:

  • elements (Set<Object>, Array<Object>)

    set elements



353
354
355
356
357
# File 'lib/ruby/rego/value.rb', line 353

def initialize(elements)
  collection = elements.is_a?(Set) ? elements.to_a : Array(elements)
  @elements = Set.new(collection.map { |element| Value.from_ruby(element) })
  super(@elements)
end

Instance Method Details

#include?(value) ⇒ Boolean

Check whether the set includes a value.

Parameters:

  • value (Object)

    value to check

Returns:

  • (Boolean)


363
364
365
# File 'lib/ruby/rego/value.rb', line 363

def include?(value)
  @elements.include?(Value.from_ruby(value))
end

#to_rubySet<Object>

Convert the set back to Ruby.

Returns:

  • (Set<Object>)


370
371
372
# File 'lib/ruby/rego/value.rb', line 370

def to_ruby
  Set.new(@elements.map(&:to_ruby))
end