Class: Upl::Atom

Inherits:
Object show all
Defined in:
lib/upl/atom.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(atom_t) ⇒ Atom

NOTE these are atom_t, NOT term_t and NOT Fiddle::Pointer to atom_t



4
5
6
7
8
9
# File 'lib/upl/atom.rb', line 4

def initialize( atom_t )
  @atom_t = atom_t

  # pretty much all other methods need chars, so just do it now.
  @chars = (::Upl::Extern::PL_atom_chars @atom_t).to_s.freeze
end

Instance Attribute Details

#atom_tObject (readonly)

Returns the value of attribute atom_t.



18
19
20
# File 'lib/upl/atom.rb', line 18

def atom_t
  @atom_t
end

Class Method Details

.of_term(term_t) ⇒ Object

drop the term immediately, and just keep the atom value



12
13
14
15
16
# File 'lib/upl/atom.rb', line 12

def self.of_term( term_t )
  rv = Extern::PL_get_atom term_t, (atom_t = Fiddle::Pointer[0].ref)
  rv == 1 or raise "can't get atom from term"
  new atom_t.ptr
end

.t_of_ruby(obj) ⇒ Object

NOTE this returns an atom_t for obj.object_id embedded in an atom



21
22
23
# File 'lib/upl/atom.rb', line 21

def self.t_of_ruby obj
  Upl::Extern.PL_new_atom "ruby-#{obj.object_id.to_s}"
end

Instance Method Details

#==(rhs) ⇒ Object



55
56
57
# File 'lib/upl/atom.rb', line 55

def == rhs
  atom_t == rhs.atom_t
end

#inspectObject



67
# File 'lib/upl/atom.rb', line 67

def inspect; to_sym end

#pretty_print(pp) ⇒ Object



69
70
71
72
73
# File 'lib/upl/atom.rb', line 69

def pretty_print pp
  pp.text atom_t
  pp.text '-'
  pp.text to_s
end

#to_obj_idObject

return the object_id embedded in the atom, or nil if it’s not an embedded object_id



27
28
29
30
31
32
33
34
35
36
# File 'lib/upl/atom.rb', line 27

def to_obj_id
  if instance_variable_defined? :@to_obj_id
    @to_obj_id
  else
    @to_obj_id =
    if @chars =~ /^ruby-(\d+)/
      $1.to_i
    end
  end
end

#to_rubyObject

return the ruby object associated with the object_id embedded in the atom



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/upl/atom.rb', line 39

def to_ruby
  if to_obj_id
    ObjectSpace._id2ref to_obj_id
  else
    case _sym = to_sym
    when :false; false
    when :true; true
    else _sym
    end
  end
rescue RangeError
  # object with the given obj_id no longer exists in ruby, so just return
  # the symbol with the embedded object_id.
  to_sym
end

#to_sObject



63
64
65
# File 'lib/upl/atom.rb', line 63

def to_s
  @chars
end

#to_symObject



59
60
61
# File 'lib/upl/atom.rb', line 59

def to_sym
  @chars.to_sym
end