Skip to content

Commit 8a6c33a

Browse files
committedJul 26, 2016
Module#define_method accepts attr_accessor methods. Fixes #3685.
1 parent 80d617d commit 8a6c33a

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed
 

‎core/method.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,20 @@ def super_method
337337
def for_define_method(name, klass, callable_proc = nil)
338338
Rubinius::Type.bindable_method? self.defined_in, klass
339339

340-
scope = @executable.scope
341-
342-
if @executable.is_a? Rubinius::DelegatedMethod
340+
case @executable
341+
when Rubinius::AccessVariable
342+
code = @executable
343+
scope = nil
344+
when Rubinius::DelegatedMethod
343345
code = @executable
346+
scope = code.scope
344347
else
345348
if callable_proc
346349
code = Rubinius::DelegatedMethod.new(name, :call, callable_proc, false)
347350
else
348351
code = Rubinius::DelegatedMethod.new(name, :call_on_instance, self, true)
349352
end
353+
scope = @executable.scope
350354
end
351355

352356
[code, scope]

‎spec/ruby/core/module/define_method_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,23 @@ def inspect_data
180180
lambda{o.other_inspect}.should raise_error(NoMethodError)
181181
end
182182

183+
it "accepts an UnboundMethod from an attr_accessor method" do
184+
class DefineMethodSpecClass
185+
attr_accessor :accessor_method
186+
end
187+
188+
m = DefineMethodSpecClass.instance_method(:accessor_method)
189+
o = DefineMethodSpecClass.new
190+
191+
DefineMethodSpecClass.send(:undef_method, :accessor_method)
192+
lambda { o.accessor_method }.should raise_error(NoMethodError)
193+
194+
DefineMethodSpecClass.send(:define_method, :accessor_method, m)
195+
196+
o.accessor_method = :abc
197+
o.accessor_method.should == :abc
198+
end
199+
183200
it "accepts a proc from a method" do
184201
class ProcFromMethod
185202
attr_accessor :data

0 commit comments

Comments
 (0)