This library provides an easy way to extract comments from a ruby source file.
$ gem install getcomments
Or with bundler:
gem 'getcomments'
Get comments from a file:
# GetComments.from_file
require 'getcomments'
comments = GetComments.from_file 'spec/fixtures/minimal.rb'
comments.each do |key, value|
puts "#{key}: #{value}"
end
#=> module TestModule: Module comment
#=> class TestClass: Class comment
#=> attr_reader :some_attr: Attribute comment
#=> def some_method: Method comment
Get comments from a string:
# GetComments.from_string
code = <<-EOF
# This function just sits here
def the_function
end
EOF
comments = GetComments.from_string code
comments.each do |key, value|
puts "#{key}: #{value}"
end
#=> def the_function: This function just sits here