ruby-changes:31680
From: nobu <ko1@a...>
Date: Thu, 21 Nov 2013 18:47:42 +0900 (JST)
Subject: [ruby-changes:31680] nobu:r43759 (trunk): delegate.rb: check if target is set
nobu 2013-11-21 18:47:31 +0900 (Thu, 21 Nov 2013) New Revision: 43759 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43759 Log: delegate.rb: check if target is set * lib/delegate.rb (SimpleDelegator#__getobj__): target object must be set. * lib/delegate.rb (DelegateClass#__getobj__): ditto. Modified files: trunk/ChangeLog trunk/lib/delegate.rb trunk/test/test_delegate.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 43758) +++ ChangeLog (revision 43759) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Nov 21 18:47:29 2013 Nobuyoshi Nakada <nobu@r...> + + * lib/delegate.rb (SimpleDelegator#__getobj__): target object must be set. + + * lib/delegate.rb (DelegateClass#__getobj__): ditto. + Thu Nov 21 18:28:42 2013 Nobuyoshi Nakada <nobu@r...> * lib/tempfile.rb (Tempfile#initialize): use class method to get rid Index: lib/delegate.rb =================================================================== --- lib/delegate.rb (revision 43758) +++ lib/delegate.rb (revision 43759) @@ -43,11 +43,14 @@ https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L43 class Delegator < BasicObject kernel = ::Kernel.dup kernel.class_eval do + alias __raise__ raise [:to_s,:inspect,:=~,:!~,:===,:<=>,:eql?,:hash].each do |m| undef_method m end private_instance_methods.each do |m| - next if /\Ablock_given\?\z|iterator\?\z/ =~ m + if /\Ablock_given\?\z|iterator\?\z|\A__raise__\z/ =~ m + next + end undef_method m end end @@ -152,7 +155,7 @@ class Delegator < BasicObject https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L155 # method calls are being delegated to. # def __getobj__ - raise NotImplementedError, "need to define `__getobj__'" + __raise__ ::NotImplementedError, "need to define `__getobj__'" end # @@ -160,7 +163,7 @@ class Delegator < BasicObject https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L163 # to _obj_. # def __setobj__(obj) - raise NotImplementedError, "need to define `__setobj__'" + __raise__ ::NotImplementedError, "need to define `__setobj__'" end # @@ -303,6 +306,7 @@ end https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L306 class SimpleDelegator<Delegator # Returns the current object method calls are being delegated to. def __getobj__ + __raise__ ::ArgumentError, "not delegated" unless defined?(@delegate_sd_obj) @delegate_sd_obj end @@ -321,7 +325,7 @@ class SimpleDelegator<Delegator https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L325 # puts names[1] # => Sinclair # def __setobj__(obj) - raise ArgumentError, "cannot delegate to self" if self.equal?(obj) + __raise__ ::ArgumentError, "cannot delegate to self" if self.equal?(obj) @delegate_sd_obj = obj end end @@ -377,10 +381,11 @@ def DelegateClass(superclass) https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L381 methods -= [:to_s,:inspect,:=~,:!~,:===] klass.module_eval do def __getobj__ # :nodoc: + __raise__ ::ArgumentError, "not delegated" unless defined?(@delegate_dc_obj) @delegate_dc_obj end def __setobj__(obj) # :nodoc: - raise ArgumentError, "cannot delegate to self" if self.equal?(obj) + __raise__ ::ArgumentError, "cannot delegate to self" if self.equal?(obj) @delegate_dc_obj = obj end methods.each do |method| Index: test/test_delegate.rb =================================================================== --- test/test_delegate.rb (revision 43758) +++ test/test_delegate.rb (revision 43759) @@ -154,4 +154,18 @@ class TestDelegateClass < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/test_delegate.rb#L154 end.new(Object.new) assert_equal("foo", d.send(:foo)) end + + def test_unset_simple_delegator + d = SimpleDelegator.allocate + assert_raise_with_message(ArgumentError, /not delegated/) { + d.__getobj__ + } + end + + def test_unset_delegate_class + d = IV.allocate + assert_raise_with_message(ArgumentError, /not delegated/) { + d.__getobj__ + } + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/