From: Aldric Giacomoni Date: 2009-11-19T23:54:29+09:00 Subject: Re: How to strip ruby comments in a ruby line of code? Aldric Giacomoni wrote: > Alexandre Mutel wrote: >> Aldric Giacomoni wrote: >>> I'm still only learning regular expressions (I'll do another shameless >>> plug for rubular.com here), but you could do this: >>> string = string.match(/^.*#).to_s[0...-1] >>> >>> Yes, it's a poor solution, but should you have nothing else, it'll do. >> >> the problem with your solution is that this line of code will remove >> valid code : >> myvar_s = "#{myvar}" >> >> The problem is to handle correctly string escape sequence... it's >> possible, but it requires much more work... I just want to know if >> someone else did this?! > > Actually, no, because regexps are greedy by default, so it'll go to the > very last '#' it finds. file # => array containing each line of the file you want to clean up file.map! do |line| line =~ /(^.*)#/ $1 end -- Posted via http://www.ruby-forum.com/.