aboutsummaryrefslogtreecommitdiffstats
path: root/proc.c
diff options
context:
space:
mode:
authorOKURA Masafumi <masafumi.o1988@gmail.com>2019-08-16 02:21:17 +0900
committerTakashi Kokubun <takashikkbn@gmail.com>2019-08-16 02:21:17 +0900
commit74726691bada2f2061c57169f4c36a50a9f500ab (patch)
tree35eb0bcb5f6b8ddcce0d48b73e215ee289ab9c08 /proc.c
parent409ce8c3da966ac8fb809bc6317990f2b5b6479d (diff)
downloadruby-74726691bada2f2061c57169f4c36a50a9f500ab.tar.gz
Add details about parameters to define_method doc (#2165)
When we use `define_method` and `define_singleton_method`, if we supply block parameters to a block then a generated method has corresponding parameters. However, the doc doesn't mention it, so this info has been added.
Diffstat (limited to 'proc.c')
-rw-r--r--proc.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/proc.c b/proc.c
index 40628f1268..810fe79c92 100644
--- a/proc.c
+++ b/proc.c
@@ -1935,8 +1935,10 @@ rb_mod_public_instance_method(VALUE mod, VALUE vid)
*
* Defines an instance method in the receiver. The _method_
* parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
- * If a block is specified, it is used as the method body. This block
- * is evaluated using #instance_eval.
+ * If a block is specified, it is used as the method body.
+ * If a block or the _method_ parameter has parameters,
+ * they're used as method parameters.
+ * This block is evaluated using #instance_eval.
*
* class A
* def fred
@@ -1946,6 +1948,7 @@ rb_mod_public_instance_method(VALUE mod, VALUE vid)
* self.class.define_method(name, &block)
* end
* define_method(:wilma) { puts "Charge it!" }
+ * define_method(:flint) {|name| puts "I'm #{name}!"}
* end
* class B < A
* define_method(:barney, instance_method(:fred))
@@ -1953,6 +1956,7 @@ rb_mod_public_instance_method(VALUE mod, VALUE vid)
* a = B.new
* a.barney
* a.wilma
+ * a.flint('Dino')
* a.create_method(:betty) { p self }
* a.betty
*
@@ -1960,6 +1964,7 @@ rb_mod_public_instance_method(VALUE mod, VALUE vid)
*
* In Fred
* Charge it!
+ * I'm Dino!
* #<B:0x401b39e8>
*/
@@ -2064,6 +2069,7 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
* Defines a singleton method in the receiver. The _method_
* parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
* If a block is specified, it is used as the method body.
+ * If a block or a method has parameters, they're used as method parameters.
*
* class A
* class << self
@@ -2080,6 +2086,10 @@ rb_mod_define_method(int argc, VALUE *argv, VALUE mod)
* guy = "Bob"
* guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
* guy.hello #=> "Bob: Hello there!"
+ *
+ * chris = "Chris"
+ * chris.define_singleton_method(:greet) {|greeting| "#{greeting}, I'm Chris!" }
+ * chris.greet("Hi") #=> "Hi, I'm Chris!"
*/
static VALUE