[#950] ruby 1.1b0 released — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

33 messages 1997/12/05

[#998] ruby 1.1b1 released — matz@... (Yukihiro Matsumoto)

まつもと ゆきひろです

48 messages 1997/12/09
[#1011] Re: ruby 1.1b1 released — Kazuhisa Yanagawa <katze@...> 1997/12/10

in message-id: <199712090833.RAA31727@picachu.netlab.co.jp>

[#1013] Re: ruby 1.1b1 released — matz@... (Yukihiro Matsumoto) 1997/12/10

まつもと ゆきひろです

[#1028] Regexp#operators (Re: ruby 1.1b1 released) — Shin-ichiro HARA <sinara@...> 1997/12/10

原です。

[#1030] Re: Regexp#operators (Re: ruby 1.1b1 released) — matz@... (Yukihiro Matsumoto) 1997/12/10

まつもと ゆきひろです

[#1033] Re: Regexp#operators (Re: ruby 1.1b1 released) — Shin-ichiro HARA <sinara@...> 1997/12/10

原です。

[#1049] Re: Regexp#operators (Re: ruby 1.1b1 released) — Shin-ichiro HARA <sinara@...> 1997/12/11

原です。

[#1064] Re: Regexp#operators (Re: ruby 1.1b1 released) — matz@... (Yukihiro Matsumoto) 1997/12/12

まつもと ゆきひろです

[#1097] Re: Regexp#operators (Re: ruby 1.1b1 released) — Shin-ichiro HARA <sinara@...> 1997/12/15

原です。

[#1002] Object#bind — shugo@... (Shugo Maeda)

前田です。

39 messages 1997/12/09
[#1008] Re: Object#bind — matz@... (Yukihiro Matsumoto) 1997/12/10

まつもと ゆきひろです

[#1023] Re: Object#bind — shugo@... (Shugo Maeda) 1997/12/10

前田です。

[#1026] Re: Object#bind — matz@... (Yukihiro Matsumoto) 1997/12/10

まつもと ゆきひろです

[#1044] Re: Object#bind — keiju@... (石塚圭樹 ) 1997/12/11

けいじゅ@日本ラショナルソフトウェアです.

[#1051] Re: Object#bind — shugo@... (Shugo Maeda) 1997/12/11

前田です。

[#1063] Re: Object#bind — matz@... (Yukihiro Matsumoto) 1997/12/12

まつもと ゆきひろです

[#1079] Re: Object#bind — keiju@... (石塚圭樹 ) 1997/12/14

けいじゅ@日本ラショナルソフトウェアです.

[#1084] Re: Object#bind — matz@... (Yukihiro Matsumoto) 1997/12/15

まつもと ゆきひろです

[#1087] Re: Object#bind — keiju@... (石塚圭樹 ) 1997/12/15

けいじゅ@日本ラショナルソフトウェアです.

[#1088] Re: Object#bind — matz@... (Yukihiro Matsumoto) 1997/12/15

まつもと ゆきひろです

[#1085] [Req] object refference — keiju@... (Keiju ISHITSUKA)

けいじゅ@日本ラショナルソフトウェアです.

12 messages 1997/12/15

[ruby-dev:1094] Re: [Req] object refference

From: matz@... (Yukihiro Matsumoto)
Date: 1997-12-15 08:07:22 UTC
List: ruby-dev #1094
まつもと ゆきひろです

In message "[ruby-dev:1091] Re: [Req] object refference"
    on 97/12/15, Yukihiro Matsumoto <matz@netlab.co.jp> writes:

|まつもと ゆきひろです

|finalizerを使えばできますね.ということはRubyレベルで書ける?

とりあえず以下のようなのはいかがでしょう.
委譲用のクラスDelegaterを使ってます.

-- weakref.rb
# Weak Reference class that does not bother GCing.
#
# Usage:
#   foo = Object.new
#   foo = WeakRef.new(foo)
#   ...
#   ObjectSpace.garbage_collect
#   ...
#   foo.hash # => Raises WeakRef::RefError (because original GC'ed)

require "delegate"

class WeakRef<Delegater

  Exception :RefError

  ID_MAP =  {}
  ID_REV_MAP =  {}
  ObjectSpace.add_finalizer(lambda{|id|
			      rid = ID_MAP[id]
			      if rid
				ID_REV_MAP[rid] = nil
				ID_MAP[id] = nil
			      end
			      rid = ID_REV_MAP[id]
			      if rid
				ID_REV_MAP[id] = nil
				ID_MAP[rid] = nil
			      end
			    })
			    
  def initialize(orig)
    @id = orig.id
    ObjectSpace.call_finalizer orig
    ID_MAP[@id] = self.id
    ID_REV_MAP[self.id] = @id
  end

  def __getobj__
    unless ID_MAP[@id]
      $@ = caller(1)
      $! = RefError.new("Illegal Reference - probably recycled")
      raise
    end

    ObjectSpace.each_object do |obj|
      return obj if obj.id == @id
    end
  end

  def alive?
    if ID_MAP[@id]
      true
    else
      false
    end
  end

  def []
    __getobj__
  end
end

-- delegate.rb
# Delegation class that delegates even methods defined for Object class.
#   which can not covered with normal method_missing hack.
#
# Usage:
#   foo = Object.new
#   foo = Delegater.new(foo)
#   foo.type # => Object

class Delegater

  def initialize(obj)
    @obj = obj
  end

  def __getobj__
    @obj
  end

  def method_missing(mid, *args)
    __getobj__.send mid, *args
  end

  DELEGATING_METHODS = [
    "nil?", "==", "===", "=~", "eql?", "hash", "type",
    "clone", "dup", "to_a", "to_s", "inspect", "methods",
    "singleton_methods", "private_methods", "instance_variables",
    "remove_instance_variable", "instance_of?", "kind_of?", "is_a?" ]

  for method in DELEGATING_METHODS
    eval "def #{method}(*args); __getobj__.send :#{method}, *args; end"
  end

end

In This Thread