Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby: instantiate objects from files
    primarykey
    data
    text
    <p>Overview:</p> <pre><code>main.rb items/ one.rb two.rb three.rb </code></pre> <p>Every file in <code>items/</code> should have a human readable description (serialization is out), like so (but maybe a DSL would be better?):</p> <pre><code>class One &lt; BaseItem name "Item one" def meth "something" end </code></pre> <p><code>main.rb</code> should be able to instantiate all objects from the <code>items/</code> directory. How could this be accomplished? Not familiar with Ruby, I see the object model allows for some pretty cool things (those class hooks, etc), but I'm having trouble finding a way to solve this.</p> <p>Any input way appreciated.</p> <p>EDIT:</p> <p>Shoot, I may have missed the gist of it - what I didn't mention was the stuff in the <code>items/</code> dir would be dynamic — treat items as plugins, I'd want <code>main.rb</code> to autodetect everything in that dir at runtime (possibly force a reload during execution). <code>main.rb</code> has no prior knowledge of the objects in there, it just knows what methods to expect from them.</p> <p>I've looked at building DSLs, considering defining (in <code>main.rb</code>) a <code>spawn</code> function that takes a block. A sample file in <code>items/</code> would look something like:</p> <pre><code>spawn do name "Item name" def foo "!" end end </code></pre> <p>And the innards of <code>spawn</code> would create a new object of the base type and pass the block to <code>instance_eval</code>. That meant I'd need to have a method <code>name</code> to set the value, but incidentally, I also wanted the value to be accessible under <code>name</code>, so I had to go around it renaming the attr. </p> <p>I've also tried the inherit route: make every item file contain a class that inherits from a <code>BaseItem</code> of sorts, and hook into it via <code>inherited</code> ... but that didn't work (the hook never fired, I've lost the code now).</p> <p>EDIT2:</p> <p>You could look at what <a href="https://github.com/mxcl/homebrew" rel="nofollow">homebrew</a> does with its formulas, that's very close to what I'd want - I just didn't have the ruby prowess to reverse engineer how it handles a formula. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload