Ruby Docstrings
Friday, March 23rd, 2007I can’t be the first person to come up with this, but since Google doesn’t show anything, I figured I’d share it.
Take the following code:
class Rect
d"Calculates the area of the rectangle." do
def area
length * height
end
end
d"The height of the rectangle." do
attr_accessor :height
end
d"The length of the rectangle." do
attr_accessor :length
end
end
This lets us do:
irb(main):035:0> Rect.__doc__(:length)
=> "The length of the rectangle."
irb(main):036:0> Rect.__doc__(:area)
=> "Calculates the area of the rectangle."
Yay! Docstrings! So, how does this work?