Remove comments from ruby source code?

Dear Friends,
If there any pre-defined function or ruby options available
for removing the ruby comments from the source code. Please any on help
me if you already aware of this.

Regards,
S.Vellingiri.

On Sep 1, 4:12 am, Arul hari [email protected] wrote:

Dear Friends,
If there any pre-defined function or ruby options available
for removing the ruby comments from the source code. Please any on help
me if you already aware of this.

Regards,
S.Vellingiri.

Posted viahttp://www.ruby-forum.com/.

You should be able to use something like this to get rid of normal
comments (ā€œ#ā€):

http://pastie.org/601862

then you could call it like this:

remcom(ā€œfilename.rbā€)

-Dylan

On Sep 1, 10:10 am, Joel VanderWerf [email protected] wrote:

#{name} is my name
END

ā€“
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

DOH! Canā€™t believe I forgot that! :slight_smile: This should work:

http://pastie.org/602035

Iā€™m sure there are plenty of other cases Iā€™ve missed, lemme know and I
can add em.

Dylan wrote:

You should be able to use something like this to get rid of normal
comments (ā€œ#ā€):

http://pastie.org/601862

This is a harder problemā€¦ what do you do about the following?

name = ā€œfredā€
puts <<END
#{name} is my name
END

At 2009-09-01 07:12AM, ā€œArul hariā€ wrote:

Dear Friends,
If there any pre-defined function or ruby options available
for removing the ruby comments from the source code. Please any on help
me if you already aware of this.

The ruby_parser and ruby2ruby gems will do this:

require 'ruby_parser'
require 'ruby2ruby'

code = <<END
# a class
class Simple
  # add the method "add"
  def add(n1,n2)
    n1 + n2  # return the sum
  end
end
END

parsed = Ruby2Ruby.new.process( RubyParser.new.process( code ))

puts parsed

results in:

class Simple
  def add(n1, n2)
    (n1 + n2)
  end
end
1 Like

On Sep 1, 2009, at 12:10 , Dylan wrote:

DOH! Canā€™t believe I forgot that! :slight_smile: This should work:

http://pastie.org/602035

Iā€™m sure there are plenty of other cases Iā€™ve missed, lemme know and I
can add em.

s = "

"

You need to actually parse the file.

Glenn J. wrote:

At 2009-09-01 07:12AM, ā€œArul hariā€ wrote:

Dear Friends,
If there any pre-defined function or ruby options available
for removing the ruby comments from the source code. Please any on help
me if you already aware of this.

The ruby_parser and ruby2ruby gems will do this:

require 'ruby_parser'
require 'ruby2ruby'

code = <<END
# a class
class Simple
  # add the method "add"
  def add(n1,n2)
    n1 + n2  # return the sum
  end
end
END

parsed = Ruby2Ruby.new.process( RubyParser.new.process( code ))

puts parsed

results in:

class Simple
  def add(n1, n2)
    (n1 + n2)
  end
end

Dear Friends,
Thanks to all for your prompt reply. It was really helpful to
me.

Regards,
S.Vellingiri.

1 Like

Arul hari wrote:

Glenn J. wrote:

At 2009-09-01 07:12AM, ā€œArul hariā€ wrote:

Dear Friends,
If there any pre-defined function or ruby options available
for removing the ruby comments from the source code. Please any on help
me if you already aware of this.

The ruby_parser and ruby2ruby gems will do this:

require 'ruby_parser'
require 'ruby2ruby'

code = <<END
# a class
class Simple
  # add the method "add"
  def add(n1,n2)
    n1 + n2  # return the sum
  end
end
END

parsed = Ruby2Ruby.new.process( RubyParser.new.process( code ))

puts parsed

results in:

class Simple
  def add(n1, n2)
    (n1 + n2)
  end
end

Dear Friends,
Thanks to all for your prompt reply. It was really helpful to
me.

Regards,
S.Vellingiri.

Dear Friends,
I am facing problem after removing comments.

#! => should not remove from the source code , but I am using
ruby2ruby module to remove the comments. Is there any way to not remove
the #! line.

Please anyone help me to go ahed on this. Kindly let me know it.

Regards,
S.vellingiri.

On 3/2/10, Arul hari [email protected] wrote:

Dear Friends,
I am facing problem after removing comments.

#! => should not remove from the source code , but I am using
ruby2ruby module to remove the comments. Is there any way to not remove
the #! line.

Please anyone help me to go ahed on this. Kindly let me know it.

The obvious thing to do is to special case the shebang line. Since a
shebang can only occur on the first line of a source file, just
examine the first line before munging the source and if itā€™s a
shebang, add it back after the munging is done.

Note that youā€™ll have the same issue with magic encoding lines as
well. A magic encoding line can also occur on the 1st line (or 2nd
line if the 1st line is a shebang) and should also be preserved if
present. (This is a ruby 1.9 only feature, tho. If ruby 1.9
compatibility is important to you, ruby2ruby isnā€™t going to work for
you for all kinds of other reasonsā€¦)

Glenn J. wrote in post #847746:

At 2009-09-01 07:12AM, ā€œArul hariā€ wrote:

Dear Friends,
If there any pre-defined function or ruby options available
for removing the ruby comments from the source code. Please any on help
me if you already aware of this.

The ruby_parser and ruby2ruby gems will do this:

require 'ruby_parser'
require 'ruby2ruby'

code = <<END
# a class
class Simple
  # add the method "add"
  def add(n1,n2)
    n1 + n2  # return the sum
  end
end
END

parsed = Ruby2Ruby.new.process( RubyParser.new.process( code ))

puts parsed

results in:

class Simple
  def add(n1, n2)
    (n1 + n2)
  end
end

Hi,

Sorry for bumping so old post but Iā€™m getting different result than
shown above. In my case I still see comments which are alone in the line
and comment which is following code is removed. Any idea why?
Iā€™m using ruby 1.9.3 with gems:
ruby2ruby (2.0.8)
ruby_parser (3.1.0)
sexp_processor (4.1.0)

Thx,
solution seems to fix some problems but still comment ā€˜# add the method
ā€œaddā€ā€™ isnā€™t stripped. But now it shall be enough to remove them with
RegExp, right?

Jakub S. wrote in post #1142466:

Sorry for bumping so old post but Iā€™m getting different result than
shown above. In my case I still see comments

this code do some patch to ruiby2ruiby , most comments are stripped :

require ā€˜ruby_parserā€™
require ā€˜ruby2rubyā€™

class Ruby2Ruby
def process_class(exp) ā€œclass #{util_module_or_class(exp, true)}ā€ end
def process_module(exp) ā€œmodule #{util_module_or_class(exp)}ā€ end
def indent(s) s end
end

src=ARGV.map {|f| ā€œfrom_file(ā€™#{f}ā€™)\nā€+File.read(f) }.join("\n\n")
puts Ruby2Ruby.new.process( RubyParser.new.process( ā€œdef from_file(e)
end\nā€+ src ))

some comments at begining of each files seem echoedā€¦