Class: Peruby::Ref

Inherits:
Object
  • Object
show all
Defined in:
lib/peruby/runtime/ref.rb

Overview

Perl reference with an optional blessed package.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, blessed = nil) ⇒ Ref

Returns a new instance of Ref.



10
11
12
13
14
# File 'lib/peruby/runtime/ref.rb', line 10

def initialize(target, blessed = nil)
  @target = target
  @blessed = blessed
  @weak = false
end

Instance Attribute Details

#blessedObject

Returns the value of attribute blessed.



8
9
10
# File 'lib/peruby/runtime/ref.rb', line 8

def blessed
  @blessed
end

#runtimeObject

Returns the value of attribute runtime.



8
9
10
# File 'lib/peruby/runtime/ref.rb', line 8

def runtime
  @runtime
end

Instance Method Details

#kindObject



30
31
32
33
34
35
36
37
# File 'lib/peruby/runtime/ref.rb', line 30

def kind
  value = target
  return 'REF' if value.is_a?(Scalar) && value.get.is_a?(Ref)

  kinds = { Scalar => 'SCALAR', PerlArray => 'ARRAY', PerlHash => 'HASH', Glob => 'GLOB',
            Regexp => 'Regexp', Code => 'CODE' }
  kinds.fetch(value.class, nil)
end

#ref_stringObject



39
40
41
# File 'lib/peruby/runtime/ref.rb', line 39

def ref_string
  @blessed || kind
end

#targetObject



16
17
18
19
20
# File 'lib/peruby/runtime/ref.rb', line 16

def target
  @weak ? @target.__getobj__ : @target
rescue WeakRef::RefError
  nil
end

#weak?Boolean

Returns:

  • (Boolean)


28
# File 'lib/peruby/runtime/ref.rb', line 28

def weak? = @weak

#weaken!Object



22
23
24
25
26
# File 'lib/peruby/runtime/ref.rb', line 22

def weaken!
  @target = WeakRef.new(@target)
  @weak = true
  self
end