Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to strip ruby comments in a ruby line of code?

Reply
Thread Tools

How to strip ruby comments in a ruby line of code?

 
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      11-19-2009
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      11-19-2009
Alexandre Mutel wrote:
> Woop, i was to fast. In fact, i need an eval on the whole file, because
> my dsl language allow ruby code to be used (and so definition of
> methods... etc.)


Then it sounds like you just need to separate the blocks of code
appropriately. Do you want each line which begins with \w: (a labelled
line) to be treated specially? Then the rest of the code between the
labelled lines can be treated as a single string.

Proof-of-concept:

src = <<EOS
def foo
puts "XXX"
end
label1: foo # this is a test
def bar
puts "YYY"
end
label2: bar
EOS

def label(name)
puts "Executing label #{name} now..."
yield
end

b = binding
line = 1
src.split(/^(\w+:.*)\n/).each do |chunk|
if chunk =~ /(\w+).*)$/
eval "label(#{$1.inspect}) { #{$2}\n }", b, "DSL", line
line += 1
else
eval chunk, b, "DSL", line
line += chunk.split("\n").size
end
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Alexandre Mutel
Guest
Posts: n/a
 
      11-19-2009
Brian Candler wrote:
> Then it sounds like you just need to separate the blocks of code
> appropriately. Do you want each line which begins with \w: (a labelled
> line) to be treated specially? Then the rest of the code between the
> labelled lines can be treated as a single string.
>
> b = binding
> line = 1
> src.split(/^(\w+:.*)\n/).each do |chunk|
> if chunk =~ /(\w+).*)$/
> eval "label(#{$1.inspect}) { #{$2}\n }", b, "DSL", line
> line += 1
> else
> eval chunk, b, "DSL", line
> line += chunk.split("\n").size
> end
> end


Damn, your solution was almost working well, but working on an external
file, the "eval" loose the step in the code and i'm not able to go back
to debug the dsl...
ok, i'm going probably to forgot about this option for now... i'll see
later on how to do it.

Thanks again Brian.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      11-19-2009
Alexandre Mutel wrote:
> Damn, your solution was almost working well, but working on an external
> file, the "eval" loose the step in the code


What do you mean by "loose the step" - it's reporting the wrong line
number? I hacked together that code very quickly, and I'm sure it's
fixable. Here is a more verbose version that is more likely to have the
correct line number.

buf = nil
buf_line = 0
b = binding
src.each_with_index do |line,i|
if line =~ /^(\w+).*)\n/
label, code = $1, $2
if buf
eval buf, b, "DSL", buf_line+1
buf = nil
end
eval "label(#{label.inspect}) { #{code}\n}", b, "DSL", i+1
else
unless buf
buf = ""
buf_line = i
end
buf << line
end
end
if buf
eval buf, b, "DSL", buf_line+1
buf = nil
end
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Alexandre Mutel
Guest
Posts: n/a
 
      11-19-2009
Brian Candler wrote:
> Alexandre Mutel wrote:
>> Damn, your solution was almost working well, but working on an external
>> file, the "eval" loose the step in the code

>
> What do you mean by "loose the step" - it's reporting the wrong line
> number? I hacked together that code very quickly, and I'm sure it's
> fixable. Here is a more verbose version that is more likely to have the
> correct line number.


i mean that before the eval of a chunk, i'm still in the dsl code, but
after i press "F8" hit, the debugger is going back to the line just
after the eval (line += chunk.split("\n").size), although i didn't setup
any breakpoint code there... it's weird, but then, I'm not able to come
back and step in the dsl code (even if i put some breakpoints).
I don't know if it's a bug or limitation on my debugger (i'm using
RubyMine) or probably I'm missing something...



--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      11-19-2009
Oh I see - eval doesn't work with a ruby debugger. I guess the debugger
is assuming that the line number in the exception backtrace is an offset
from the start of the eval string, which it isn't here.

I did think of another and simpler solution for you though. When you
insert a newline and close-brace, add a semicolon and not another
newline. e.g.

n: foo(bar) # comment
nextline

becomes:

label(:n) { foo(bar) # comment
}; nextline

How would that be?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Alexandre Mutel
Guest
Posts: n/a
 
      11-19-2009
Brian Candler wrote:
> n: foo(bar) # comment
> nextline
>
> becomes:
>
> label(:n) { foo(bar) # comment
> }; nextline
>
> How would that be?

YES! it seems to work perfectly... the ; doesn't alter the line counting
for the debugger. In fact, I tried this solution this morning without
the semicolon... but yep, it's logical with semicolon now!

Thanks very much Brian, this is helping me a lot.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
strip all but second second line from bottom and then strip that!!!! yelipolok Perl Misc 4 01-27-2010 08:14 AM
Comments format: comments extending over multi-line Monk C Programming 10 04-20-2005 05:09 PM
strip and its evil brother strip! Aquila Ruby 35 03-31-2005 04:10 AM
Strip all comments KiwiBrian HTML 6 07-23-2004 06:28 PM



Advertisments