Skip to content

Commit 20c5335

Browse files
authored
Merge pull request #1431 from ruby/define-singleton-method-proc
Let `define_singleton_method` accept a proc
2 parents 6ddd05d + 21868f4 commit 20c5335

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

core/module.rbs

+2-2
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,8 @@ class Module < Object
726726
# I'm Dino!
727727
# #<B:0x401b39e8>
728728
#
729-
def define_method: (id arg0, ?Proc | Method | UnboundMethod arg1) -> Symbol
730-
| (id arg0) { () -> untyped } -> Symbol
729+
def define_method: (id symbol, Proc | Method | UnboundMethod method) -> Symbol
730+
| (id symbol) { () -> untyped } -> Symbol
731731

732732
# <!--
733733
# rdoc-file=object.c

core/object.rbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class Object < BasicObject
205205
# chris.define_singleton_method(:greet) {|greeting| "#{greeting}, I'm Chris!" }
206206
# chris.greet("Hi") #=> "Hi, I'm Chris!"
207207
#
208-
def define_singleton_method: (name, Method | UnboundMethod) -> Symbol
208+
def define_singleton_method: (name, Method | UnboundMethod | Proc method) -> Symbol
209209
| (name) { (*untyped) -> untyped } -> Symbol
210210

211211
# <!--

test/stdlib/Object_test.rb

+38
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,41 @@ def test_to_s
206206
Object.new.to_s
207207
end
208208
end
209+
210+
211+
class ObjectInstanceTest < Test::Unit::TestCase
212+
include TypeAssertions
213+
214+
testing "::Object"
215+
216+
def test_define_singleton_method
217+
obj = Object.new
218+
219+
assert_send_type(
220+
"(::Symbol) { () -> void } -> Symbol",
221+
obj, :define_singleton_method,
222+
:foo
223+
) do end
224+
225+
assert_send_type(
226+
"(::Symbol, ::Proc) -> Symbol",
227+
obj, :define_singleton_method,
228+
:bar,
229+
-> {}
230+
)
231+
232+
assert_send_type(
233+
"(::Symbol, ::Method) -> Symbol",
234+
obj, :define_singleton_method,
235+
:bar,
236+
obj.method(:to_s)
237+
)
238+
239+
assert_send_type(
240+
"(::Symbol, ::UnboundMethod) -> Symbol",
241+
obj, :define_singleton_method,
242+
:bar,
243+
Object.instance_method(:to_s)
244+
)
245+
end
246+
end

0 commit comments

Comments
 (0)