Ignore commented out code when using YARD
Asked Answered
T

1

6

I have some Ruby code that looks like this:

# some_string = "{really?}"

where the curly braces need to be part of the string. This line is commented out code that I'd like to remain there. I'm additionally using YARD to document code, so when I run yard doc it (naturally) throws a warning about being unable to link "really".

Is there a way I can tell YARD to ignore commented out code?

Thyrotoxicosis answered 2/12, 2013 at 21:26 Comment(2)
What are you keeping the code for? There may be an alternative to commented-out code that serves the same purpose for you, and is less effort than whatever fiddling you need to do with YARD.Textualism
No good reason. I normally just delete commented out chunks of code (that's what revision control is for), but it seems like this ought to be possible. Plus sometimes commented out code could be left in there for a good reason, though I can't think of one right now.Thyrotoxicosis
S
3

Is there a way I can tell YARD to ignore commented out code?

On the one hand, YARD is documented as supporting Rdoc markup. And Rdoc is documented to support a couple of ways to hide parts.

RDoc stops processing comments if it finds a comment line starting with -- right after the # character (otherwise, it will be treated as a rule if it has three dashes or more). This can be used to separate external from internal comments, or to stop a comment being associated with a method, class, or module. Commenting can be turned back on with a line that starts with ++.

:stopdoc: / :startdoc:
Stop and start adding new documentation elements to the current container. For example, if a class has a number of constants that you don’t want to document, put a :stopdoc: before the first, and a :startdoc: after the last. If you don’t specify a :startdoc: by the end of the container, disables documentation for the rest of the current file.

Source

On the other hand, I have never persuaded Rdoc or YARD to follow that markup. If your luck is better than mine, you can stop reading here.

If you, too, can't persuade YARD to follow that markup, I think your best bet might be to cut that line, and commit the file with a distinctive commit message--one that you'll be able to find easily by grepping the source control logs.

Finally, rake lets you transform text (code) files in arbitrary ways. You can write a Rakefile to delete lines before processing them through YARD.

$ cat silly-ruby-file.src 
class Something

  def this_method
  end

  def that_method
    # some_string = "{really?}" # Hide me
  end
end

I appended the text # Hide me; it's a lot easier to filter that specific text than it is to filter commented lines of arbitrary code.

$ cat Rakefile
task :default => "silly-ruby-file.rb"
sh "grep -v '# Hide me' silly-ruby-file.src > silly-ruby-file.rb"

This tells rake to run grep, copying all lines except those that have the text "# Hide me" to stdout, which is redirected to "silly-ruby-file.rb".

Slapbang answered 28/2, 2015 at 3:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.