Class: RDF::Query::Binding

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/query/binding.rb

Overview

A single binding of variables in an Graph.

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ Binding

Creates a new instange of Binding initializing it with hash, or an empty Hash if hash is nil.



6
7
8
# File 'lib/rdf/query/binding.rb', line 6

def initialize(hash = nil)
  @hash = hash || {}
end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?

Returns true if o is a Binding and it has the same bindings.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rdf/query/binding.rb', line 36

def ==(o)
  if o.is_a?(Binding)
    @hash.each_pair do |k,v|
      return false unless o[k] == v
    end
    
    ohash = o.instance_variable_get('@hash')
    ohash.each_pair do |k,v|
      return false unless @hash[k] == v
    end
    
    true
  end
end

#[](key) ⇒ Object

Returns the value bound to key if key is bound in this binding, or key otherwise.



21
22
23
# File 'lib/rdf/query/binding.rb', line 21

def [](key)
  @hash.key?(key) ? @hash[key] : key
end

#[]=(key, value) ⇒ Object

Binds key to value in this binding.



26
27
28
# File 'lib/rdf/query/binding.rb', line 26

def []=(key, value)
  @hash[key] = value
end

#bound?(key) ⇒ Boolean

Returns true if key is bound in this binding.

Returns:

  • (Boolean)


16
17
18
# File 'lib/rdf/query/binding.rb', line 16

def bound?(key)
  @hash.key?(key)
end

#empty?Boolean

Returns true if this binding is empty, false otherwise.

Returns:

  • (Boolean)


31
32
33
# File 'lib/rdf/query/binding.rb', line 31

def empty?
  @hash.empty?
end

#hashObject

Returns a hash value for this binding.



54
55
56
57
58
59
60
61
# File 'lib/rdf/query/binding.rb', line 54

def hash
  h = -901662846
  @hash.each_pair do |k,v|
    h ^= (h << 5) + k.hash + (h >> 2)
    h ^= (h << 5) + v.hash + (h >> 2)
  end
  h
end

#initialize_copy(o) ⇒ Object

Copy constructor that creates a deep copy of this binding.



11
12
13
# File 'lib/rdf/query/binding.rb', line 11

def initialize_copy(o)
  @hash = o.instance_variable_get('@hash').clone
end

#to_sObject



63
64
65
# File 'lib/rdf/query/binding.rb', line 63

def to_s
  "{#{@hash.collect{|k,v| "#{k} => #{v}"}.join(", ")}}"
end