0

The yard gem is a tool for generating docs of ruby code.

It's done via the command line, and the docs get generated.

However I was wondering if it's possible to interact with the parsed code and statistic via an IRB.

You can go into the IRB and call up yard like this:

require 'yard'
YARD

However I can't seem to interact with the code or get and parsed stats. For example getting the list of methods in the code would be great, or a object of method lists via the parser.

Docs: (http://rubydoc.info/gems/yard/YARD)

CafeHey
  • 5,699
  • 19
  • 82
  • 145

3 Answers3

2

See documentation on the YARD::Registry (linked to from the Architecture Overview document) for accessing code objects that got parsed out after you ran yard.

Example for printing all methods in your registry:

require 'yard'

YARD::Registry.load!
puts YARD::Registry.all(:method).map(&:path)

Advanced class-- getting all undocumented objects:

require 'yard'

YARD::Registry.load!
puts YARD::Registry.all.select {|o| o.docstring.blank? }.map(&:path)

You can see more properties of CodeObjects in the CodeObjects Architecture Overview in addition to the YARD::CodeObjects::Base class API docs. That will give you more information on what you can query. You may also want to look at the YARD::Docstring class if you plan on introspecting tags.

Note that if you want to actually generate the registry (you haven't yet run yard), you can do so with the Registry class, but it would probably be better to use YARD::CLI::Yardoc.run for that.

Loren Segal
  • 3,251
  • 1
  • 28
  • 29
0

Looking to the yard source it seems like the gem is not meant to be use with that purpose but you can extract some info.

require 'yard'

stats = YARD::CLI::Stats.new
stats.run(files) # it allows patter matching, for instance 'lib/*.rb'
stats.all_objects # all the objects recognized by the parser

stats.all_objects.select{|o| o.type == :method} # to get only methods
stats.all_objects.select{|o| o.type == :class} # to get only classes

I'm not sure if that is enough for you but I don't think you can get information in a deeper level.

javiyu
  • 1,336
  • 6
  • 9
  • This always results in 0 stats or an empty array, no matter what file paths params I pass into .run(files) – CafeHey Sep 17 '14 at 08:37
  • YARD is absolutely meant to be used programmatically. There is a whole API and guide about that use case: http://rubydoc.info/gems/yard/YARD/Registry – Loren Segal Sep 20 '14 at 09:43
0

It really depends on what you need to do, but I strongly suggest you to take a look at pry that lets you do great things:

[1] pry(main)> require 'cgi'
=> true
[2] pry(main)> show-method CGI::escape

From: /home/carlesso/.rbenv/versions/2.1.2/lib/ruby/2.1.0/cgi/util.rb @ line 7:
Owner: CGI::Util
Visibility: public
Number of lines: 6

def escape(string)
  encoding = string.encoding
  string.b.gsub(/([^ a-zA-Z0-9_.-]+)/) do |m|
    '%' + m.unpack('H2' * m.bytesize).join('%').upcase
  end.tr(' ', '+').force_encoding(encoding)
end

and even more strange stuff:

[4] pry(main)> cd CGI
[5] pry(CGI):1> ls
constants: 
  Cookie  CR  EOL  HtmlExtension  HTTP_STATUS  InvalidEncoding  LF  MAX_MULTIPART_COUNT  MAX_MULTIPART_LENGTH  NEEDS_BINMODE  PATH_SEPARATOR  QueryExtension  REVISION  Util
Object.methods: yaml_tag
CGI::Util#methods: 
  escape  escapeElement  escapeHTML  escape_element  escape_html  h  pretty  rfc1123_date  unescape  unescapeElement  unescapeHTML  unescape_element  unescape_html
CGI.methods: accept_charset  accept_charset=  parse
CGI#methods: accept_charset  header  http_header  nph?  out  print
class variables: @@accept_charset
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_

you can also edit something, like edit CGI::escape will open your $EDITOR to the relevant file/line (in my case, vim will be opened at .rbenv/versions/2.1.2/lib/ruby/2.1.0/cgi/util.rb line 7

Where present will show the help:

[10] pry(CGI):1> help Pry.hist
Usage:   hist [--head|--tail]
         hist --all
         hist --head N
         hist --tail N
         hist --show START..END
         hist --grep PATTERN
         hist --clear
         hist --replay START..END
         hist --save [START..END] FILE
Aliases: history

Show and replay Readline history.

    -a, --all              Display all history
    -H, --head             Display the first N items
    -T, --tail             Display the last N items
    -s, --show             Show the given range of lines
    -G, --grep             Show lines matching the given pattern
    -c, --clear            Clear the current session's history
    -r, --replay           Replay a line or range of lines
        --save             Save history to a file
    -e, --exclude-pry      Exclude Pry commands from the history
    -n, --no-numbers       Omit line numbers
    -h, --help             Show this message.

But, again, it really depends on your needs, a little bit of "metaprogramming" can help you, like .methods, .instance_variables, .constants can be useful..

Enrico Carlesso
  • 6,818
  • 4
  • 34
  • 42
  • Yeah I thought about pry, but yard gets all the classes and categorizes them, it looks at the code comments and gets descriptions / params ect. It's specifically the yard stats and info I want to manipulate. – CafeHey Sep 18 '14 at 10:18