Skip to content

Instantly share code, notes, and snippets.

@eddieantonio
Created May 21, 2015 05:09
Show Gist options
  • Save eddieantonio/c7d533bfac66f678b730 to your computer and use it in GitHub Desktop.
Save eddieantonio/c7d533bfac66f678b730 to your computer and use it in GitHub Desktop.
class Object
# Opens a browser window showing the documentation for the given
# class or method on ruby-doc.org.
#
# Usage:
#
# instance.doc -> Goes to class documentation.
# instance.doc :method -> Goes to method documentation.
def doc(name = nil)
cls = self.class
# Evaluate there to use all of the helpers in this module.
RubyDocOrg.module_eval do
url = name.nil? ? class_url(cls) : method_url(cls, name)
launch_browser url
end
nil
end
end
# Helper methods.
# Avoid polluting the global namespace by stuffing the helper methods in here:
module RubyDocOrg
def self.url_for(class_name, method_name = nil)
prefix = "http://ruby-doc.org/core-#{RUBY_VERSION}"
class_page = "#{class_name}.html"
if method_name.nil?
method_part = ''
else
method_escaped = method_name
.gsub(/[^A-Za-z_]/) { |c| c.ord.to_s(16).upcase + '-'}
.chomp('-')
method_part = "##{method_escaped}-method"
end
"#{prefix}/#{class_page}#{method_part}"
end
# Define the appropriate browser... thing.
case RUBY_PLATFORM
when /darwin/i
def self.launch_browser(url)
system('open', url)
end
when /linux/i
def self.launch_browser(url)
system('xdg-open', url)
end
else
raise "I don't know how to launch the browser in #{RUBY_PLATFORM}..."
end
def self.class_url(cls)
class_name = cls.to_s
url_for(class_name)
end
def self.method_url(cls, name)
ancestor = cls.ancestors.find { |parent|
parent.instance_methods(false).include? name
}
raise "Could not find :#{name} in #{cls}!" if ancestor.nil?
class_name = ancestor.to_s
method_name = name.to_s
url_for(class_name, method_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment