mirror of
https://github.com/ruby/ruby.git
synced 2026-01-27 04:24:23 +00:00
[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.
This commit is contained in:
parent
806031d2ce
commit
189bb64af8
Notes:
git
2026-01-15 02:36:45 +00:00
36
proc.c
36
proc.c
@ -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
|
||||
*
|
||||
* interpreter = Interpreter.new
|
||||
* interpreter.interpret('dave')
|
||||
* def hi
|
||||
* puts "Hi, I'm #{@name}!"
|
||||
* end
|
||||
* end
|
||||
*
|
||||
* dave = Person.new('Dave')
|
||||
* thomas = Person.new('Thomas')
|
||||
*
|
||||
* 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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user