Remove comments from ruby source code?

Discussion in 'Ruby' started by Arul hari, Sep 1, 2009.

  1. Arul hari

    Arul hari Guest

    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 via http://www.ruby-forum.com/.
     
    Arul hari, Sep 1, 2009
    #1
    1. Advertising

  2. Arul hari

    Dylan Guest

    On Sep 1, 4:12 am, 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.
    >
    >   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
     
    Dylan, Sep 1, 2009
    #2
    1. Advertising

  3. 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

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Sep 1, 2009
    #3
  4. Arul hari

    Dylan Guest

    On Sep 1, 10:10 am, Joel VanderWerf <> wrote:
    > 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
    >
    > --
    >        vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


    DOH! Can't believe I forgot that! :) 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, Sep 1, 2009
    #4
  5. Arul hari

    Ryan Davis Guest

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

    > DOH! Can't believe I forgot that! :) 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.
     
    Ryan Davis, Sep 1, 2009
    #5
  6. Arul hari

    Arul hari Guest

    Glenn Jackman 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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Arul hari, Sep 2, 2009
    #6
  7. Arul hari

    Arul hari Guest

    Arul hari wrote:
    > Glenn Jackman 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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Arul hari, Mar 2, 2010
    #7
  8. On 3/2/10, Arul hari <> 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....)
     
    Caleb Clausen, Mar 2, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,132
  2. Monk
    Replies:
    10
    Views:
    1,476
    Michael Wojcik
    Apr 20, 2005
  3. Alexandre Mutel
    Replies:
    16
    Views:
    785
    Alexandre Mutel
    Nov 19, 2009
  4. Replies:
    4
    Views:
    611
    Dr John Stockton
    Jun 3, 2006
  5. F.F.
    Replies:
    3
    Views:
    321
    Tim Rentsch
    Sep 22, 2013
Loading...

Share This Page