From: Brian Candler Date: 2009-11-19T23:32:08+09:00 Subject: Re: How to strip ruby comments in a ruby line of code? Alexandre Mutel wrote: > I could put a newline after the match like this : > newLabel(:my_label) { here_is_a_dsl(arg1, arg2) # my comments > } > > Unfornutately, i'm no longer able to debug my dsl language, because the > lines are not matching the preprocessed line. However if you eval each line individually, then you can pass in the source line number. def foo; end src = "foo\nfoo\nbar" src.each_with_index do |line,i| eval "#{line} {\n}", binding, "DSL", i+1 end # Result: DSL:3: undefined method `bar' for main:Object (NoMethodError) Otherwise, if every input line maps to exactly two output lines, you can just patch up the line number in the exception by dividing by two. src = "foo\nfoo\nbar\n" begin eval src.gsub(/\n/, "{\n}\n"), binding, "DSL", 1 rescue => e if e.backtrace.first =~ /\A(.*):(\d+)\z/ e.backtrace.first.replace "#{$1}:#{($2.to_i+1) / 2}" end raise e end -- Posted via http://www.ruby-forum.com/.