LoginSignup
5
3

More than 5 years have passed since last update.

Rubyの最長API名を探してみた

Last updated at Posted at 2013-08-10

http://qiita.com/yohhoy/items/b61d175e161ce3493096
が面白かったので、自動で取れないか試してみたらそれっぽいのが・・・

定数名+メソッド名の長さで、被りとか細かいことは気にしないという前提で書いたものがこれ

# ruby -v: ruby 2.1.0dev (2013-08-10 trunk 42476) [x86_64-linux]

$VERBOSE = true

class BasicObject
  def longest_method_name
    (methods(true) | private_methods(true)).reject { |name|
      /\Alongest_(?:instance_)?method_name\z/ =~ name }.max_by(&:length)
  end
end

class Module
  def longest_instance_method_name
    (instance_methods(true) | private_instance_methods(true)).reject { |name|
      /\Alongest_(?:instance_)?method_name\z/ =~ name }.max_by(&:length)
  end

  def longest_api(ignore_modules=[])
    apis = [[name, longest_instance_method_name].join('#')]
    constants.each do |const_name|
      const = const_get const_name
      if const.kind_of?(Module) && !ignore_modules.include?(const)
        apis << [const.name, longest_method_name].join('.')
        ignore_modules << const
        apis << const.__send__(__callee__, ignore_modules)
      else
        apis << [[name, const_name].join("::"), longest_method_name].join('.')
      end
    end

    apis.max_by(&:length)
  end
end

p Object.longest_api
#=> Gem::Specification::NONEXISTENT_SPECIFICATION_VERSION.outdated_and_latest_version
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3