2

How can I get the list of available languages (the short symbol to be passed when calling a method that represents the language) for CodeRay syntax highlighter?

I tried

require "coderay"
CodeRay::Scanners.constants

but that doesn't seem to give the information. (Even if I were able to get the constants that correspond to the languages, I still need another step to get the symbols that correspond to them.)


A related question is, I can do something like:

CodeRay::Scanners::Ruby # => CodeRay::Scanners::Ruby

but CodeRay::Scanners.constants doesn't include that. Why is that?

sawa
  • 165,429
  • 45
  • 277
  • 381
  • 1
    `CodeRay::Scanners::Ruby` seems to be autoloaded. Once called, it is included in `CodeRay::Scanners.constants`. – Stefan Apr 26 '16 at 09:35
  • 1
    If you have two questions, you should ask two questions. As it stands now, you have an answer to one of your questions but not the other one. If you had asked them separately, you would probably have gotten an answer to both. From glancing at the code, it seems to use both [`autoload`](http://ruby-doc.org/core/Module.html#method-i-autoload) and [`const_missing`](http://ruby-doc.org/core/Module.html#method-i-const_missing). – Jörg W Mittag Apr 26 '16 at 09:37

1 Answers1

3

Method you are looking for is:

CodeRay::Scanners.list
#=> [:c, :clojure, :cpp, :css, :debug, :delphi, :diff, :erb, :go, :groovy,
#    :haml, :html, :java, :java_script, :json, :lua, :php, :python, :raydebug,
#    :ruby, :sass, :scanner, :sql, :taskpaper, :text, :xml, :yaml]
Stefan
  • 109,145
  • 14
  • 143
  • 218
ksarunas
  • 1,187
  • 2
  • 11
  • 16
  • 1
    Thank you ksarunas. And thanks to Stefan too, I saw your post. Thanks to Jörg W Mittag too, `CodeRay::PluginHost::plugin_hash` seems to be related. – sawa Apr 26 '16 at 09:33
  • 1
    BTW, `list` scans the filesystem for available plugins without (auto)loading the files. – Stefan Apr 26 '16 at 09:40