From: Trans Date: 2005-09-06T06:56:27+09:00 Subject: Re: Change Binding of a Proc Florian Gro� wrote: > Trans wrote: > > > I'm able to turn the proc into an instance method, then capture the > > UnboundMethod, then undefine it, then bind it to the instance in order > > to call it, but that sure seems like a hek of a round about way to have > > to do things. > > I wonder why you have to call a proc on an instance when it isn't used > as an instance method. Care to explain? I'm writing a little parser and one defines a "state-machine" for the parser to use. In it one defines tokens. The tokens can be paired having a start and end regexp, but they can be in procs so that they can react to parsing state. For example: class MyMachine < StateParser::StateMachine token :xnx, /x(.)x/, proc{ |match,state| /y#{match[1]}y/ } end The above would match on something like "x1x ... y1y". But also the machine can have event hooks, and with those can also do the above like this: class MyMachine < StateParser::StateMachine def initialize @stack = [] end token :xnx, /x(.)x/, proc{ |match,state| /y#{@stack.last}y/ } def xnx( match, state ) puts "I just matched #{match[0]}" @stack << match[1] end def end_xnx( match, state ) puts "I just end matched #{match[0]}" @stack.pop end end > > Any better solutions? > > evil-ruby has Proc#self=. It's not one of the safer methods. Beleive me, my current solution is evil enough as it is ;-) T.