class RDF::Query::Variable
An RDF query variable.
@example Creating a named unbound variable
var = RDF::Query::Variable.new(:x) var.unbound? #=> true var.value #=> nil
@example Creating an anonymous unbound variable
var = RDF::Query::Variable.new var.name #=> :g2166151240
@example Unbound variables match any value
var === RDF::Literal(42) #=> true
@example Creating a bound variable
var = RDF::Query::Variable.new(:y, 123) var.bound? #=> true var.value #=> 123
@example Bound variables match only their actual value
var = RDF::Query::Variable.new(:y, 123) var === 42 #=> false var === 123 #=> true
@example Getting the variable name
var = RDF::Query::Variable.new(:y, 123) var.named? #=> true var.name #=> :y var.to_sym #=> :y
@example Rebinding a variable returns the previous value
var.bind!(456) #=> 123 var.value #=> 456
@example Unbinding a previously bound variable
var.unbind! var.unbound? #=> true
@example Getting the string representation of a variable
var = RDF::Query::Variable.new(:x) var.to_s #=> "?x" var = RDF::Query::Variable.new(:y, 123) var.to_s #=> "?y=123"
Attributes
The variable's name.
@return [Symbol]
The variable's name.
@return [Symbol]
The variable's value.
@return [RDF::Term]
Public Class Methods
@param [Symbol, to_sym] name
the variable name
@param [RDF::Term] value
an optional variable value
# File lib/rdf/query/variable.rb, line 68 def initialize(name = nil, value = nil) @name = (name || "g#{__id__.to_i.abs}").to_sym @value = value end
Public Instance Methods
Compares this variable with the given value.
@param [RDF::Term] other @return [Boolean]
# File lib/rdf/query/variable.rb, line 196 def ===(other) if unbound? other.is_a?(RDF::Term) # match any Term when unbound else value === other end end
Rebinds this variable to the given `value`.
@param [RDF::Term] value @return [RDF::Term] the previous value, if any.
# File lib/rdf/query/variable.rb, line 129 def bind(value) old_value = self.value self.value = value old_value end
Returns this variable's bindings (if any) as a `Hash`.
@return [Hash{Symbol => RDF::Term}]
# File lib/rdf/query/variable.rb, line 160 def bindings unbound? ? {} : {name => value} end
Returns `true` if this variable is bound.
@return [Boolean]
# File lib/rdf/query/variable.rb, line 95 def bound? !unbound? end
Sets if variable is distinguished or non-distinguished. By default, variables are distinguished
@return [Boolean]
# File lib/rdf/query/variable.rb, line 120 def distinguished=(value) @distinguished = value end
Returns `true` if this variable is distinguished.
@return [Boolean]
# File lib/rdf/query/variable.rb, line 111 def distinguished? @distinguished.nil? || @distinguished end
Returns `true` if this variable is equivalent to a given `other` variable. Or, to another Term if bound, or to any other Term
@param [Object] other @return [Boolean] `true` or `false` @since 0.3.0
# File lib/rdf/query/variable.rb, line 180 def eql?(other) if unbound? other.is_a?(RDF::Term) # match any Term when unbound elsif other.is_a?(RDF::Query::Variable) @name.eql?(other.name) else value.eql?(other) end end
Returns a hash code for this variable.
@return [Fixnum] @since 0.3.0
# File lib/rdf/query/variable.rb, line 169 def hash @name.hash end
Returns `true` if this variable has a name.
@return [Boolean]
# File lib/rdf/query/variable.rb, line 87 def named? true end
Returns a string representation of this variable.
Distinguished variables are indicated with a single `?`.
Non-distinguished variables are indicated with a double `??`
@example
v = Variable.new("a") v.to_s => '?a' v.distinguished = false v.to_s => '??a'
@return [String]
# File lib/rdf/query/variable.rb, line 218 def to_s prefix = distinguished? ? '?' : "??" unbound? ? "#{prefix}#{name}" : "#{prefix}#{name}=#{value}" end
Unbinds this variable, discarding any currently bound value.
@return [RDF::Term] the previous value, if any.
# File lib/rdf/query/variable.rb, line 140 def unbind old_value = self.value self.value = nil old_value end
Returns `true` if this variable is unbound.
@return [Boolean]
# File lib/rdf/query/variable.rb, line 103 def unbound? value.nil? end
Returns `true`.
@return [Boolean] @see RDF::Value#variable? @since 0.1.7
# File lib/rdf/query/variable.rb, line 79 def variable? true end
Returns this variable as `Hash`.
@return [Hash{Symbol => RDF::Query::Variable}]
# File lib/rdf/query/variable.rb, line 151 def variables {name => self} end