From: cldwalker Date: 2009-07-25T20:50:09+09:00 Subject: Re: thoughts on rewriting irb? On Jul 24, 6:36 pm, Joel VanderWerf wrote: > cldwalker wrote: > > On Jul 23, 9:47 pm, Joel VanderWerf wrote: > >> ghorner wrote: > >>>   begin puts "=> #{eval(input).inspect}"; rescue Exception; puts > >> Do you want > > >> eval(input, b) > > >> where b is a Binding that was constructed before the loop? Otherwise, > >> how will this support local vars? > > > Joel, > >   not sure I follow. In mini-irb this works: > > >   >> b = 2 > >   => 2 > >   >> 'b' * b > >   => "bb" > > > As does placing local variables before the loop being picked up by > > eval. Could you elaborate? > > Gabriel > > Here's a simplified version of your code: > > $ cat mirb.rb > %w{readline rubygems}.each {|e| require e } > while (input = Readline.readline('>> ', true)) != 'exit' >    begin puts "=> #{eval(input).inspect}"; rescue Exception; puts > "Error: #{$!}" end >   end > $ ruby mirb.rb >  >> input > => "input" > > This is fixable using bindings: > > $ cat mirb.rb > %w{readline rubygems}.each {|e| require e } > def get_binding; binding; end > b = get_binding > while (input = Readline.readline('>> ', true)) != 'exit' >    begin puts "=> #{eval(input, b).inspect}"; rescue Exception; puts > "Error: #{$!}" end >   end > $ ruby mirb.rb >  >> input > >  >> > > (Now, some might say that's still a bug, but at least it doesn't expose > a local variable of mini-irb.) Thanks for pointing that out. Would you know why irb does some funkier binding with its IRB.conf[:CONTEXT_MODE] option i.e. binding to a temporary file? I asked about it here, http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/6aa102dba71850f3, but no one seemed to know. Now with proper binding, mini-irb stands at less than 10 lines. So I'm still asking, what of the 5000+ loc in irb do people actually use?