tag:blogger.com,1999:blog-12467669.post482493923935239723..comments2023-04-29T07:23:25.825-04:00Comments on Jay Fields' Thoughts: Ruby: Dwemthy's Array using ModulesJayhttp://www.blogger.com/profile/14491442812573747680noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-12467669.post-350722951997308972008-08-26T19:56:00.000-04:002008-08-26T19:56:00.000-04:00A pastie may make that clearer: http://rafb.net/p/...A pastie may make that clearer: http://rafb.net/p/XlSByD47.html<BR/><BR/>JerryJerryhttps://www.blogger.com/profile/17010019463192281234noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-60472947484047840322008-08-26T19:55:00.000-04:002008-08-26T19:55:00.000-04:00Interesting; now the traits are isolated in a modu...Interesting; now the traits are isolated in a module, you can share them between classes. For this implementation, pull #initialize from #traits and put it into the top level of Creature (where it belongs?). Move the attr_accessor(*arr) into the loop over traits in #initialize (hmmm, bit of a hack using self.class.send). Now export the module (e.g. make <I>mod</I> an instance variable <B>@mod</B> and add a class level accessor).<BR/><BR/>Now you can declare class Hare < Creature; extend Rabbit.mod; bombs 6; end<BR/><BR/>Or more interesting: class Chimera < Creature; extend Lion.mod, Goat.mod, Serpent.mod; end<BR/><BR/>best wishes,<BR/> JerryJerryhttps://www.blogger.com/profile/17010019463192281234noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-79629593593160324882008-07-28T12:36:00.000-04:002008-07-28T12:36:00.000-04:00Thank you for these responses. Including a module ...Thank you for these responses. Including a module in a module was a nice trick, nkryptic, I'll have to play with that code some more though to really absorb it. I'm glad my question was useful to others, I was wondering if I was just being thick. Thank you for your patience.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-36931747133539549592008-07-28T09:28:00.000-04:002008-07-28T09:28:00.000-04:00Thanks for the quick turn-around in answering that...Thanks for the quick turn-around in answering that commenter's question. It was helpful to see an example of refactoring something I had seen before, using your recommendations.mileszshttps://www.blogger.com/profile/11545285708958141180noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-81002053002719215842008-07-27T20:02:00.000-04:002008-07-27T20:02:00.000-04:00Hi mr anonymous,I'm guessing that the confusion is...Hi mr anonymous,<BR/><BR/>I'm guessing that the confusion is due to the use of arr in the Module.new definition, which abuses the fact that arr is in scope.<BR/><BR/>Just a guess, but a great blog post anyway. Bathing in the coding style of merb, you'd love the simplistic style than the "magical" one.Unknownhttps://www.blogger.com/profile/14866674283251994502noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-58380056884265579692008-07-26T23:23:00.000-04:002008-07-26T23:23:00.000-04:00In response to the anonymous comment prior, I've c...In response to the anonymous comment prior, I've created <A HREF="http://pastie.org/241853" REL="nofollow">a pastie which shows an includable Traits module</A>. Hope that helps.Unknownhttps://www.blogger.com/profile/12281247079807729921noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-20057816272062196552008-07-25T13:07:00.000-04:002008-07-25T13:07:00.000-04:00Thank you. That was quicker than Monday! I think...Thank you. That was quicker than Monday! I think the module version has helped me understand the metaclass version. <BR/><BR/>What I can't seem to do is to factor out the module into an external module called Traits, which I can use like:<BR/><BR/>class Character<BR/> include Traits<BR/> traits :forgetfulness, :arrogance<BR/>end<BR/><BR/>Maybe it is because I'm getting confused about what {self, self.class} are inside and outside the method definitions of a module, especially when dynamically created. <BR/><BR/>Or, is this because a method in a module has no way to test if its module has been included in a class or not [AFAIK], so the only way to get this to work is to define it in the scope of a class? Then you can get at the variables (class or instance) to modify them specifically for that class, whereas a module defined outside a class can only get at that modules variables?<BR/><BR/>Do you want to see the ghastly horridness I have created out of your example in my attempt, given that it doesn't work?Anonymousnoreply@blogger.com