From: Robert Klemme Date: 2009-08-19T00:14:51+09:00 Subject: Re: How can I extract variables from a ruby snippet 2009/8/18 Brian Candler : > Otherwise, you could execute the template using erb, and use > method_missing to pick up attempts to access local variables. From these > return a dummy object, which again uses method_missing to see what > methods have been called on that dummy object. If the method_missing > passes a block then you will need to yield the block with dummy > arguments. I toyed around a bit - this solution is by far not perfect! #!ruby19 -w class FillIn < Object def initialize(name) @name = name.to_s end def method_missing(s,*a,&b) a.map! {|x| x.inspect} nn = case s when :[] "#{@name}[#{a.join(', ')}]" when :[]= v = a.slice!(-1, 1) "#{@name}[#{a.join(', ')}]=#{v}" else if a.empty? "#{@name}.#{s}" else "#{@name}.#{s}(#{a.join(', ')})" end end puts nn self.class.new(nn) end def to_s @name end alias inspect to_s end root = FillIn.new "root" p root root.foo.bar['help'].length root.foo.bar[FillIn.new("x")].length > This may be good enough for what you need, but isn't guaranteed to catch > all possible execution paths. e.g. what about > >  <% if foo %> >    <%= foo.xxx %> >  <% else %> >    <%= bar.yyy %> >  <% end %> > ? > > You are going to find it hard to exercise both branches of this > conditional. Similarly with loops, where the nth iteration may behave > differently to the previous ones. Correct. I feel somehow reminded of http://en.wikipedia.org/wiki/Halting_problem :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/