1

I have the following class, which I want to save all the desc fields into an array.

I'm going to be using this in multiple classes, so I thought to save some typing, I would use the module Describe.

module Describe
  @@interactive = []

  def desc(desc)
    @desc = desc
  end

  def method_added(method)
    @@interactive.push(@desc)
    @desc = nil
  end
end

This is what it looks like in the class.

class Dog
  extend Describe

  desc "Holy"
  def bark
    puts "woof"
  end

  desc "Moly"
  def wag
    puts "wagging"
  end
end

Here is how I plan to use the code.

d = Dog.new
puts d.interactive

But I can't access the interactive array. I can't use a class, because if I try to use inheritance, the function desc isn't found. The same occurs when I try to use include instead of extend.

Is there any way for me to access the interactive array if I continue to use extend? Am I going about this problem incorrectly? Is there a way to make include run before the rest of my code so that the method desc is found?

What I'm trying to accomplish is dynamic documentation. I am trying to return to another program that I cannot control, the method name, the parameters and the description of each of these methods. I only asked about the description in the above question as the main issue I am having is that I do not know how to access the variable. I could just use code comments and parse those, but I wanted to make it clear to any programmer that came after me what my intent was and to prevent any unintentional comment accidents.

4
  • What is the point of @@interactive? What do you want to do with it? As is, you accumulate strings, but don't refer to the variable. Are you trying to use it for dynamic documentation? Also, why does method_added(method) take a method parameter but not use it? Commented Jun 24, 2013 at 0:43
  • I think your interpretation is correct, but if you could link me to what you consider to be dynamic documentation, then I could let you know for sure. Commented Jun 24, 2013 at 0:48
  • Dynamic documentation would be the opposite of static documentation, where it's created once, similar to what happens when we run rdoc or ri. Dynamic would be created on the fly by the running program, but I have no idea how it'd be used or why because it's in your code and you don't explain how you intend to use it. Commented Jun 24, 2013 at 0:57
  • Sorry, I did not see your edited comment. I have updated the question. Hopefully, this has clarified my intent enough. Please let me know if I should elaborate. Commented Jun 24, 2013 at 1:33

2 Answers 2

1

Hope this helps:

module Describe
  @@interactive = []

  def interactive
    @@interactive
  end

  def desc(desc)
    @@interactive << desc
  end
end

class Dog
  extend Describe

  desc "Holy"
  def bark
    puts "woof"
  end

  desc "Moly"
  def wag
    puts "wagging"
  end
end


puts Dog.interactive.inspect   #["Holy", "Moly"]
Sign up to request clarification or add additional context in comments.

5 Comments

Calling Dog.interactive.inspect works only before I call Dog.new. Any ideas why? I will edit my post to clarify how I'm using interactive, as well as my motivation.
@Seanny123 Can’t replicate that, Dog.interactive works fine before or after initializing an object.
What version of Ruby are you using Andrew?
Hey @Seanny123 works for me too. I am using 1.9.3, but just tested it in 1.8.7 and it worked fine there too. Which version of Ruby are you using?
I apologize for wasting everyone's time. I realized I was calling interactive.inspect on the instance of Dog instead of the class itself. Your code works and I have accepted the answer.
1

Instead of trying to do this inside the code, I'd run rdoc against the codebase, then use:

`ri method`

to retrieve the information for it or tell them to use ri from the command-line to get documentation.

That'll take advantage of well-tested techniques and tools in Ruby and is how we're supposed to document our code.

See the rdoc documentation for formatting information.

5 Comments

I will consider this alternative approach after I fix my ri installation.
Although I have accepted the other answer, since it is more generally applicable, I will apply your methodology to solve my problem. Since I have to create documentation anyways, it only makes sense that I re-use what I typed. Thank you for your help.
It should be noted that as a result of 'ri' and the Yard equivalent 'yri' being extremely slow, if you're going to retrieve the information more than once and if you're going to have more than a dozen methods, you should not use this methodology.
You can always start up gem server and redirect them to the appropriate page, or use the HTML output generated by rdoc and serve that.
Later on, I found out that you can make custom templates with YARD. So one could create a custom HTML page with all of the required data and parse that instead of parsing multiple pages. Hopefully this helps anyone who is going down the same path as me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.