From: Marnen Laibow-Koser Date: 2009-11-19T23:57:22+09:00 Subject: Re: How to strip ruby comments in a ruby line of code? Aldric Giacomoni wrote: > Alexandre Mutel wrote: >> 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? >> >> 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! > > 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. It's not possible to do this reliably with regular experessions, because of the interaction of # with quoting constructs. You'll need a parser (Treetop can help make one). Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.