diff options
| author | OKURA Masafumi <masafumi.o1988@gmail.com> | 2023-12-14 17:52:27 +0900 |
|---|---|---|
| committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2026-01-15 11:36:18 +0900 |
| commit | 189bb64af8951d79bd812f2dca18a0104339b56e (patch) | |
| tree | 22c2803808983eacf37f702463b5b24ee5247968 | |
| parent | 806031d2ce9cb26e146ce47f6811ca038f1ffe27 (diff) | |
[ci-skip] Shorter example for `Module#instance_method`
The previous example code was too complex and includes extra logics
that's not relevant to its main usage: `bind`.
The new example code focuses on `bind_call` so that readers can
understand how it works more easily.
| -rw-r--r-- | proc.c | 36 |
1 files changed, 18 insertions, 18 deletions
@@ -2371,29 +2371,29 @@ rb_obj_singleton_method(VALUE obj, VALUE vid) * * Returns an +UnboundMethod+ representing the given * instance method in _mod_. + * See +UnboundMethod+ about how to utilize it * - * class Interpreter - * def do_a() print "there, "; end - * def do_d() print "Hello "; end - * def do_e() print "!\n"; end - * def do_v() print "Dave"; end - * Dispatcher = { - * "a" => instance_method(:do_a), - * "d" => instance_method(:do_d), - * "e" => instance_method(:do_e), - * "v" => instance_method(:do_v) - * } - * def interpret(string) - * string.each_char {|b| Dispatcher[b].bind(self).call } - * end - * end + * class Person + * def initialize(name) + * @name = name + * end + * + * def hi + * puts "Hi, I'm #{@name}!" + * end + * end + * + * dave = Person.new('Dave') + * thomas = Person.new('Thomas') * - * interpreter = Interpreter.new - * interpreter.interpret('dave') + * hi = Person.instance_method(:hi) + * hi.bind_call(dave) + * hi.bind_call(thomas) * * <em>produces:</em> * - * Hello there, Dave! + * Hi, I'm Dave! + * Hi, I'm Thomas! */ static VALUE |
