best way for removing comment from ruby program

Hi all,

I would want to remove all comments from my ruby/rails program. It may seem like a simple task, but it is not so. Because you need to have your tool implemented as like your language parser which is actually not so easy.

And am in the search of it, to remove comment from ruby/rails. Hoping help from somebody to suggest about proven way to remove all comments from the source file..

I already searched for some options in ruby command, and it is not there. And googled too.. but unable to. So kindly help me out in achieving this..

Can you put some sample code and whts ur expected output

Single line comments:

grep -v '^#'

For everything else:

perl -ne 'BEGIN{ $skip=0; } next if /^#[^!]/; if(/^=begin/){$skip=1; next;} if(/^=end/){$skip=0; next;} next if $skip; print' yourfile.rb

If i have not communicated the requirement correctly, let me do it now.

I can write Perl programs to remove the comment, but it is tough to handle all the cases such as,

  1. removing single line comment,
  2. removing multi line comment,
  3. removing comments which are in the side of code

It should not remove the code wrongly, because in ruby,

  1. we can access variables using #.
  2. A string which can contain # in it.
    And lot other cases which i dont know. As i want to use this way for a sensitive application, the implementation which i do should not affect the code.

So i want to know about a proven tool or best method to remove source from ruby/rails program. Where as for php we have two standard ways as -w option, or a function in php can parse and give the tokens.

For perl, removing comment in perl is by"perltidy -dac".

I am searching for such a standard way for ruby/rails. Thanks for your time.

When i started to search for a more promising way than implementing it by myself, i got the way as:

ruby_parser and ruby2ruby gems will do this

It is from a ruby forum, if somebody needy can check out here.
Remove comments from ruby source code? - Ruby Forum

So the right way for removing comment from a programming language is, ask the interpreter/compiler of that language which only can do it exactly !!

Anyway thanks for all your time, Cheers.