Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: weakref.rb
require "delegate"
[0] Fix | Delete
[1] Fix | Delete
# Weak Reference class that allows a referenced object to be
[2] Fix | Delete
# garbage-collected.
[3] Fix | Delete
#
[4] Fix | Delete
# A WeakRef may be used exactly like the object it references.
[5] Fix | Delete
#
[6] Fix | Delete
# Usage:
[7] Fix | Delete
#
[8] Fix | Delete
# foo = Object.new # create a new object instance
[9] Fix | Delete
# p foo.to_s # original's class
[10] Fix | Delete
# foo = WeakRef.new(foo) # reassign foo with WeakRef instance
[11] Fix | Delete
# p foo.to_s # should be same class
[12] Fix | Delete
# GC.start # start the garbage collector
[13] Fix | Delete
# p foo.to_s # should raise exception (recycled)
[14] Fix | Delete
#
[15] Fix | Delete
# == Example
[16] Fix | Delete
#
[17] Fix | Delete
# With help from WeakRef, we can implement our own rudimentary WeakHash class.
[18] Fix | Delete
#
[19] Fix | Delete
# We will call it WeakHash, since it's really just a Hash except all of it's
[20] Fix | Delete
# keys and values can be garbage collected.
[21] Fix | Delete
#
[22] Fix | Delete
# require 'weakref'
[23] Fix | Delete
#
[24] Fix | Delete
# class WeakHash < Hash
[25] Fix | Delete
# def []= key, obj
[26] Fix | Delete
# super WeakRef.new(key), WeakRef.new(obj)
[27] Fix | Delete
# end
[28] Fix | Delete
# end
[29] Fix | Delete
#
[30] Fix | Delete
# This is just a simple implementation, we've opened the Hash class and changed
[31] Fix | Delete
# Hash#store to create a new WeakRef object with +key+ and +obj+ parameters
[32] Fix | Delete
# before passing them as our key-value pair to the hash.
[33] Fix | Delete
#
[34] Fix | Delete
# With this you will have to limit your self to String keys, otherwise you
[35] Fix | Delete
# will get an ArgumentError because WeakRef cannot create a finalizer for a
[36] Fix | Delete
# Symbol. Symbols are immutable and cannot be garbage collected.
[37] Fix | Delete
#
[38] Fix | Delete
# Let's see it in action:
[39] Fix | Delete
#
[40] Fix | Delete
# omg = "lol"
[41] Fix | Delete
# c = WeakHash.new
[42] Fix | Delete
# c['foo'] = "bar"
[43] Fix | Delete
# c['baz'] = Object.new
[44] Fix | Delete
# c['qux'] = omg
[45] Fix | Delete
# puts c.inspect
[46] Fix | Delete
# #=> {"foo"=>"bar", "baz"=>#<Object:0x007f4ddfc6cb48>, "qux"=>"lol"}
[47] Fix | Delete
#
[48] Fix | Delete
# # Now run the garbage collector
[49] Fix | Delete
# GC.start
[50] Fix | Delete
# c['foo'] #=> nil
[51] Fix | Delete
# c['baz'] #=> nil
[52] Fix | Delete
# c['qux'] #=> nil
[53] Fix | Delete
# omg #=> "lol"
[54] Fix | Delete
#
[55] Fix | Delete
# puts c.inspect
[56] Fix | Delete
# #=> WeakRef::RefError: Invalid Reference - probably recycled
[57] Fix | Delete
#
[58] Fix | Delete
# You can see the local variable +omg+ stayed, although its reference in our
[59] Fix | Delete
# hash object was garbage collected, along with the rest of the keys and
[60] Fix | Delete
# values. Also, when we tried to inspect our hash, we got a WeakRef::RefError.
[61] Fix | Delete
# This is because these objects were also garbage collected.
[62] Fix | Delete
[63] Fix | Delete
class WeakRef < Delegator
[64] Fix | Delete
[65] Fix | Delete
##
[66] Fix | Delete
# RefError is raised when a referenced object has been recycled by the
[67] Fix | Delete
# garbage collector
[68] Fix | Delete
[69] Fix | Delete
class RefError < StandardError
[70] Fix | Delete
end
[71] Fix | Delete
[72] Fix | Delete
@@__map = ::ObjectSpace::WeakMap.new
[73] Fix | Delete
[74] Fix | Delete
##
[75] Fix | Delete
# Creates a weak reference to +orig+
[76] Fix | Delete
#
[77] Fix | Delete
# Raises an ArgumentError if the given +orig+ is immutable, such as Symbol,
[78] Fix | Delete
# Fixnum, or Float.
[79] Fix | Delete
[80] Fix | Delete
def initialize(orig)
[81] Fix | Delete
case orig
[82] Fix | Delete
when true, false, nil
[83] Fix | Delete
@delegate_sd_obj = orig
[84] Fix | Delete
else
[85] Fix | Delete
@@__map[self] = orig
[86] Fix | Delete
end
[87] Fix | Delete
super
[88] Fix | Delete
end
[89] Fix | Delete
[90] Fix | Delete
def __getobj__ # :nodoc:
[91] Fix | Delete
@@__map[self] or defined?(@delegate_sd_obj) ? @delegate_sd_obj :
[92] Fix | Delete
Kernel::raise(RefError, "Invalid Reference - probably recycled", Kernel::caller(2))
[93] Fix | Delete
end
[94] Fix | Delete
[95] Fix | Delete
def __setobj__(obj) # :nodoc:
[96] Fix | Delete
end
[97] Fix | Delete
[98] Fix | Delete
##
[99] Fix | Delete
# Returns true if the referenced object is still alive.
[100] Fix | Delete
[101] Fix | Delete
def weakref_alive?
[102] Fix | Delete
@@__map.key?(self) or defined?(@delegate_sd_obj)
[103] Fix | Delete
end
[104] Fix | Delete
end
[105] Fix | Delete
[106] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function