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

name[RW]

The variable's name.

@return [Symbol]

to_sym[RW]

The variable's name.

@return [Symbol]

value[RW]

The variable's value.

@return [RDF::Term]

Public Class Methods

new(name = nil, value = nil) click to toggle source

@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

==(other)
Alias for: eql?
===(other) click to toggle source

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
bind(value) click to toggle source

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
Also aliased as: bind!
bind!(value)
Alias for: bind
bindings() click to toggle source

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
bound?() click to toggle source

Returns `true` if this variable is bound.

@return [Boolean]

# File lib/rdf/query/variable.rb, line 95
def bound?
  !unbound?
end
distinguished=(value) click to toggle source

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
distinguished?() click to toggle source

Returns `true` if this variable is distinguished.

@return [Boolean]

# File lib/rdf/query/variable.rb, line 111
def distinguished?
  @distinguished.nil? || @distinguished
end
eql?(other) click to toggle source

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
Also aliased as: ==
hash() click to toggle source

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
named?() click to toggle source

Returns `true` if this variable has a name.

@return [Boolean]

# File lib/rdf/query/variable.rb, line 87
def named?
  true
end
to_hash()
Alias for: variables
to_s() click to toggle source

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
unbind() click to toggle source

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
Also aliased as: unbind!
unbind!()
Alias for: unbind
unbound?() click to toggle source

Returns `true` if this variable is unbound.

@return [Boolean]

# File lib/rdf/query/variable.rb, line 103
def unbound?
  value.nil?
end
variable?() click to toggle source

Returns `true`.

@return [Boolean] @see RDF::Value#variable? @since 0.1.7

# File lib/rdf/query/variable.rb, line 79
def variable? 
  true
end
variables() click to toggle source

Returns this variable as `Hash`.

@return [Hash{Symbol => RDF::Query::Variable}]

# File lib/rdf/query/variable.rb, line 151
def variables
  {name => self}
end
Also aliased as: to_hash