From: Alexandre Mutel Date: 2009-11-19T23:15:57+09:00 Subject: How to strip ruby comments in a ruby line of code? Short description : My question is : do you know any available method, giving the string of a Ruby line of code, to remove comments from this line of code? ________________ Long description : For my dsl project, i'm loading my dsl files and applying a small preprocess on each line before performing a global instance_eval on the preprocessed file. Basically, in my dsl language, it is possible to put a label followed by a ":" starting at the beginning of a line like this: my_label: here_is_a_dsl(arg1, arg2) This label may be followed by a dsl instruction. The preprocessor is transforming the previous line to this line: newLabel(:my_label) { here_is_a_dsl(arg1, arg2) } using the following code: append = "" File.open(file).each do |line| match = line.match(/^([a-zA-Z_]\w+):[\s\r\n]+(.*)/) if ( match.nil?) append += line else append += "newLabel(:#{match[1]}) { #{match[2]} }\n" end end The problem arise when there is a comment at the end of the input line : my_label: here_is_a_dsl(arg1, arg2) # my comments It's then generating the following line: newLabel(:my_label) { here_is_a_dsl(arg1, arg2) # my comments } Meanning that the "}" end block is commented and having a parse error on the whole file. 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. ----- I would like to have something really simple and not being forced to use a full ruby language parser to parse those lines and remove the comments. Any idea? Thanks! -- Posted via http://www.ruby-forum.com/.