[前][次][番号順一覧][スレッド一覧]

ruby-changes:40044

From: eregon <ko1@a...>
Date: Thu, 15 Oct 2015 01:57:55 +0900 (JST)
Subject: [ruby-changes:40044] eregon:r52125 (trunk): * lib/ostruct.rb: Finish defining OpenStruct attributes lazily.

eregon	2015-10-15 01:57:21 +0900 (Thu, 15 Oct 2015)

  New Revision: 52125

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52125

  Log:
    * lib/ostruct.rb: Finish defining OpenStruct attributes lazily.
      Patch by @sferik in [GH-1037]: https://github.com/ruby/ruby/pull/1037
      This commit is an addendum to https://github.com/ruby/ruby/pull/1033.
      It:
      1. lazily defines attribute accessors for copied and marshaled objects,
      2. returns nil when an attribute reader is not defined, and
      3. defines respond_to_missing? to maintain the same respond_to? behavior

  Modified files:
    trunk/ChangeLog
    trunk/lib/ostruct.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52124)
+++ ChangeLog	(revision 52125)
@@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Oct 15 01:49:25 2015  Benoit Daloze  <eregontp@g...>
+
+	* lib/ostruct.rb: Finish defining OpenStruct attributes lazily.
+	  Patch by @sferik in [GH-1037]: https://github.com/ruby/ruby/pull/1037
+	  This commit is an addendum to https://github.com/ruby/ruby/pull/1033.
+	  It:
+	  1. lazily defines attribute accessors for copied and marshaled objects,
+	  2. returns nil when an attribute reader is not defined, and
+	  3. defines respond_to_missing? to maintain the same respond_to? behavior
+
 Wed Oct 14 16:56:50 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* configure.in: check for libunwind.h, which is not available in
Index: lib/ostruct.rb
===================================================================
--- lib/ostruct.rb	(revision 52124)
+++ lib/ostruct.rb	(revision 52125)
@@ -98,7 +98,6 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L98
   def initialize_copy(orig)
     super
     @table = @table.dup
-    @table.each_key{|key| new_ostruct_member(key)}
   end
 
   #
@@ -140,7 +139,6 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L139
   #
   def marshal_load(x)
     @table = x
-    @table.each_key{|key| new_ostruct_member(key)}
   end
 
   #
@@ -172,6 +170,11 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L170
   end
   protected :new_ostruct_member
 
+  def respond_to_missing?(mid, include_private = false)
+    mname = mid.to_s.chomp("=").to_sym
+    @table.key?(mname) || super
+  end
+
   def method_missing(mid, *args) # :nodoc:
     len = args.length
     if mname = mid[/.*(?==\z)/m]
@@ -180,7 +183,10 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L183
       end
       modifiable[new_ostruct_member(mname)] = args[0]
     elsif len == 0
-      @table[mid]
+      if @table.key?(mid)
+        new_ostruct_member(mid)
+        @table[mid]
+      end
     else
       err = NoMethodError.new "undefined method `#{mid}' for #{self}", mid, args
       err.set_backtrace caller(1)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]