赞 | 13 |
VIP | 118 |
好人卡 | 28 |
积分 | 12 |
经验 | 35779 |
最后登录 | 2017-7-6 |
在线时间 | 1564 小时 |
Lv3.寻梦者
- 梦石
- 0
- 星屑
- 1195
- 在线时间
- 1564 小时
- 注册时间
- 2008-7-30
- 帖子
- 4418
|
回复 禾西 的帖子
就是想问问bind是什么类类提供的方法。运行了ri上关于 Class#instance_method 的示范,竟然出错。- C:\Documents and Settings\DeathKing>irb
- irb(main):001:0> class Interpreter
- irb(main):002:1> def do_a() print "there, "; end
- irb(main):003:1> def do_d() print "Hello "; end
- irb(main):004:1> def do_e() print "!\n"; end
- irb(main):005:1> def do_v() print "Dave"; end
- irb(main):006:1> Dispatcher = {
- irb(main):007:2* ?a => instance_method(:do_a),
- irb(main):008:2* ?d => instance_method(:do_d),
- irb(main):009:2* ?e => instance_method(:do_e),
- irb(main):010:2* ?v => instance_method(:do_v)
- irb(main):011:2> }
- irb(main):012:1> def interpret(string)
- irb(main):013:2> string.each_byte {|b| Dispatcher[b].bind(self).call
- }
- irb(main):014:2> end
- irb(main):015:1> end
- => nil
- irb(main):016:0>
- irb(main):017:0* interpreter = Interpreter.new
- => #<Interpreter:0xdccad0>
- irb(main):018:0> interpreter.interpret('dave')
- NoMethodError: undefined method `bind' for nil:NilClass
- from (irb):13:in `block in interpret'
- from (irb):13:in `each_byte'
- from (irb):13:in `interpret'
- from (irb):18
- from D:/Program/ruby/bin/irb.bat:20:in `<main>'
- irb(main):019:0>
复制代码 |
|