Skip to content

Instantly share code, notes, and snippets.

@mboeh
Created March 19, 2012 19:31
Show Gist options
  • Save mboeh/2125331 to your computer and use it in GitHub Desktop.
Save mboeh/2125331 to your computer and use it in GitHub Desktop.
Runtime documentation for Ruby classes
# NOTE: I just slapped this together as a proof of concept. Thread safety etc. is left to the reader.
module RTDocumentation
def self.included(into)
# method_added doesn't seem to work if mixed in
into.send(:define_method, :method_added) do |name|
if @_rtdoc_nextdoc
@_rtdocs ||= {}
@_rtdocs[name.to_s] = @_rtdoc_nextdoc
doc nil
end
end
end
def doc_for(method)
@_rtdocs ||= {}
@_rtdocs[method.to_s]
end
private
def doc(docstring)
@_rtdoc_nextdoc = docstring
end
end
class UnboundMethod
def doc
owner.doc_for(name)
end
end
class Method
def doc
owner.doc_for(name)
end
end
class Module
include RTDocumentation
end
class Worf
doc "Returns a true value if this worf is currently discommendated"
def discommendated?
true
end
end
puts Worf.doc_for(:discommendated?)
puts Worf.instance_method(:discommendated?).doc
puts Worf.new.method(:discommendated?).doc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment