[#40298] Re: [ruby-cvs:33760] Ruby:r26545 (trunk): Wed Feb 3 10:12:09 2010 Aaron Patterson <tenderlove@ruby-lang.org> — "NARUSE, Yui" <naruse@...>

成瀬です。

29 messages 2010/02/03
[#40307] Re: [ruby-cvs:33760] Ruby:r26545 (trunk): Wed Feb 3 10:12:09 2010 Aaron Patterson <tenderlove@ruby-lang.org> — KOSAKI Motohiro <kosaki.motohiro@...> 2010/02/03

小崎です

[#40339] Re: [ruby-cvs:33760] Ruby:r26545 (trunk): Wed Feb 3 10:12:09 2010 Aaron Patterson <tenderlove@ruby-lang.org> — Tanaka Akira <akr@...> 2010/02/07

2010年2月3日21:07 KOSAKI Motohiro <kosaki.motohiro@gmail.com>:

[#40345] Re: [ruby-cvs:33760] Ruby:r26545 (trunk): Wed Feb 3 10:12:09 2010 Aaron Patterson <tenderlove@ruby-lang.org> — "NARUSE, Yui" <naruse@...> 2010/02/07

成瀬です。

[#40490] Re: [ruby-cvs:33760] Ruby:r26545 (trunk): Wed Feb 3 10:12:09 2010 Aaron Patterson <tenderlove@ruby-lang.org> — "NARUSE, Yui" <naruse@...> 2010/02/25

成瀬です。

[#40511] Re: [ruby-cvs:33760] Ruby:r26545 (trunk): Wed Feb 3 10:12:09 2010 Aaron Patterson <tenderlove@ruby-lang.org> — Aaron Patterson <aaron.patterson@...> 2010/02/27

2010/2/25 NARUSE, Yui <naruse@airemix.jp>:

[#40513] Re: [ruby-cvs:33760] Ruby:r26545 (trunk): Wed Feb 3 10:12:09 2010 Aaron Patterson <tenderlove@ruby-lang.org> — "U.Nakamura" <usa@...> 2010/02/27

アーロン宛

[#40317] [Bug:trunk] TCPServer#gets gets stuck — Yusuke ENDOH <mame@...>

遠藤です。

19 messages 2010/02/04
[#40371] Re: [Bug:trunk] TCPServer#gets gets stuck — Yusuke ENDOH <mame@...> 2010/02/09

遠藤です。

[#40382] [Bug:trunk] rubyspec: ObjectSpace.define_finalizer doesn't call self-referencing finalizers FAILED — Yusuke ENDOH <mame@...>

なかださんかまつもとさん

9 messages 2010/02/10

[#40418] [Feature #2746] ビルドする拡張ライブラリを configure 時に指定するための --with-exts オプション — Kenta Murata <redmine@...>

Feature #2746: ビルドする拡張ライブラリを configure 時に指定するための --with-exts オプション

11 messages 2010/02/15

[#40461] respond_to?(<protected method name>) returns true — "Akinori MUSHA" <knu@...>

 今さらかもしれませんが、 respond_to? で protected メソッドを

14 messages 2010/02/22
[#40462] Re: respond_to?(<protected method name>) returns true — Yukihiro Matsumoto <matz@...> 2010/02/23

まつもと ゆきひろです

[#40463] Re: respond_to?(<protected method name>) returns true — "Akinori MUSHA" <knu@...> 2010/02/23

At Tue, 23 Feb 2010 14:09:52 +0900,

[#40464] Re: respond_to?(<protected method name>) returns true — Yukihiro Matsumoto <matz@...> 2010/02/23

まつもと ゆきひろです

[#40467] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "NARUSE, Yui" <naruse@...>

成瀬です。

27 messages 2010/02/23
[#40468] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "Akinori MUSHA" <knu@...> 2010/02/23

At Wed, 24 Feb 2010 01:28:24 +0900,

[#40469] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "NARUSE, Yui" <naruse@...> 2010/02/23

(2010/02/24 1:57), Akinori MUSHA wrote:

[#40470] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "Akinori MUSHA" <knu@...> 2010/02/23

At Wed, 24 Feb 2010 02:07:00 +0900,

[#40472] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "NAKAMURA, Hiroshi" <nakahiro@...> 2010/02/23

2010/2/24 Akinori MUSHA <knu@idaemons.org>:

[#40473] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "Akinori MUSHA" <knu@...> 2010/02/23

At Wed, 24 Feb 2010 06:06:13 +0900,

[#40486] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "NAKAMURA, Hiroshi" <nakahiro@...> 2010/02/25

MjAxMC8yLzI0IEFraW5vcmkgTVVTSEEgPGtudUBpZGFlbW9ucy5vcmc+Ogo+PiAbJEIwRTlmPWhN

[#41367] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "NAKAMURA, Hiroshi" <nakahiro@...> 2010/05/20

2010/2/25 NAKAMURA, Hiroshi <nakahiro@gmail.com>:

[#41373] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "Akinori MUSHA" <knu@...> 2010/05/20

 長いことほとんど Ruby に時間が割けておらずすみません。

[#41518] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "NAKAMURA, Hiroshi" <nakahiro@...> 2010/06/03

2010/5/20 Akinori MUSHA <knu@idaemons.org>:

[#41520] Re: [ruby-cvs:33954] Ruby:r26739 (trunk): * ext/openssl/ossl_digest.c (GetDigestPtr): Allow to pass the — "NARUSE, Yui" <naruse@...> 2010/06/03

成瀬です。

[#40492] [Bug #2794] Aborted (core dumped) BUG on Ruby/DL — Takao NISHI <redmine@...>

Bug #2794: Aborted (core dumped) BUG on Ruby/DL

10 messages 2010/02/26

[ruby-dev:40471] improving handling of reserved names in OpenStruct

From: "Akinori MUSHA" <knu@...>
Date: 2010-02-23 18:16:23 UTC
List: ruby-dev #40471
 OpenStruct の実装を何点か改善したいと考えています。

 1. [ruby-dev:40463]のような respond_to? の挙動に依存した部分は
    書き換えたい。protected メソッドと同名のメンバーは従来通り
    使用可能に。

 2. new_ostruct_member など、 OpenStruct の実装に使われている
    public メソッドと同名のメンバーを作ろうとしても、値を取り
    出そうとすれば予期せぬメソッドが呼ばれ、また仮に代入できて
    しまうとすれば誤動作を招くのだから、 NameError を出すべき
    ではないだろうか。

    ただ、無視してくれた方が不定データを取り込む際には便利かも
    しれないので、明示的にチェックの有無を指定させる方がよい?
    たとえば OpenStruct.new(hash, validate = false) とフラグを
    加えるなど。

 3. class や method などの Kernel モジュールメソッドと同名の
    メンバーは、ユーザが望むなら使えるべきではないか。


 関心のある方は意見をください。


 とりあえず上記の基本部分をひとまとめにしたパッチを付けます。

--
Akinori MUSHA / http://akinori.org/

Index: lib/ostruct.rb
===================================================================
--- lib/ostruct.rb	(revision 26742)
+++ lib/ostruct.rb	(working copy)
@@ -31,6 +31,11 @@
 #   p data        # -> <OpenStruct country="Australia" population=20000000>
 #
 class OpenStruct
+  METHOD_class          = Kernel.instance_method(:class)
+  METHOD_object_id      = Kernel.instance_method(:object_id)
+  METHOD_method         = Kernel.instance_method(:method)
+  METHOD_public_method  = Kernel.instance_method(:public_method)
+
   #
   # Create a new OpenStruct object.  The optional +hash+, if given, will
   # generate attributes and values.  For example.
@@ -79,12 +84,25 @@ class OpenStruct

   def new_ostruct_member(name)
     name = name.to_sym
-    unless self.respond_to?(name)
-      class << self; self; end.class_eval do
-        define_method(name) { @table[name] }
-        define_method("#{name}=") { |x| modifiable[name] = x }
+    singleton = class << self; self; end
+    if self.respond_to?(name)
+      protected_p = false
+      begin
+        owner = METHOD_public_method.bind(self).call(name).owner
+      rescue NameError
+        owner = METHOD_method.bind(self).call(name).owner
+        protected_p = true
+      end
+      if owner == singleton || protected_p
+        return name
+      elsif owner <= OpenStruct
+        raise NameError, "the name `#{name}' is reserved by implementation (#{owner})"
       end
     end
+    singleton.class_eval do
+      define_method(name) { @table[name] }
+      define_method("#{name}=") { |x| modifiable[name] = x }
+    end
     name
   end

@@ -116,14 +134,14 @@ class OpenStruct
   # Returns a string containing a detailed summary of the keys and values.
   #
   def inspect
-    str = "#<#{self.class}"
+    str = "#<#{METHOD_class.bind(self).call}"

     ids = (Thread.current[InspectKey] ||= [])
     if ids.include?(object_id)
       return str << ' ...>'
     end

-    ids << object_id
+    ids << METHOD_object_id.bind(self).call
     begin
       first = true
       for k,v in @table
Index: test/ostruct/test_ostruct.rb
===================================================================
--- test/ostruct/test_ostruct.rb	(revision 26742)
+++ test/ostruct/test_ostruct.rb	(working copy)
@@ -21,6 +21,47 @@ class TC_OpenStruct < Test::Unit::TestCa
     assert_not_equal(o1, o2)
   end

+  def test_kernel_methods
+    o = OpenStruct.new
+    assert_nothing_raised("The name 'method' should be available") {
+      o.method = :something
+      assert_equal(:something, o.method)
+    }
+
+    o = OpenStruct.new
+    o.object_id = 123
+    o.class = "Foo"
+    assert_equal("#<OpenStruct object_id=123, class=\"Foo\">", o.inspect,
+      "Setting 'class' should not affect the result of #inspect")
+  end
+
+  def test_protected_methods
+    o = OpenStruct.new
+
+    assert_nothing_raised("The name 'modifiable' should be available") {
+      o.modifiable = :something
+      assert_equal(:something, o.modifiable)
+    }
+
+    assert_nothing_raised("The name 'table' should be available") {
+      o.table = :something
+      assert_equal(:something, o.table)
+    }
+  end
+
+  def test_reserved_methods
+    assert_raises(NameError) {
+      o = OpenStruct.new
+      o.inspect = "foo"
+    }
+
+    assert_raises(NameError) {
+      subclass = Class.new(OpenStruct)
+      o = subclass.new
+      o.inspect = "foo"
+    }
+  end
+
   def test_inspect
     foo = OpenStruct.new
     assert_equal("#<OpenStruct>", foo.inspect)

In This Thread

Prev Next