tag:blogger.com,1999:blog-7821856652257554779.post2854287507951951716..comments2023-10-22T12:47:47.534+02:00Comments on Andrzej on Software: Code reuse in Ruby, why composition is so rare?Andrzej Krzywdahttp://www.blogger.com/profile/06399276063142826365noreply@blogger.comBlogger11125tag:blogger.com,1999:blog-7821856652257554779.post-49657964772392277582011-02-03T14:27:38.750+01:002011-02-03T14:27:38.750+01:00Isn't part of the issue that even with composi...Isn't part of the issue that even with composition you still have to write all the delegation code - something you just don't have to do with inheritance.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-37567253898126669322011-01-17T16:43:26.321+01:002011-01-17T16:43:26.321+01:00m.extend(m)
nice :) thanks!m.extend(m)<br /><br />nice :) thanks!Andrzej Krzywdahttps://www.blogger.com/profile/06399276063142826365noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-38705707925268715402011-01-17T16:39:16.839+01:002011-01-17T16:39:16.839+01:00A little bit of a workaround, but you can do
m = ...A little bit of a workaround, but you can do<br /><br />m = Module.new { def hello; 'world'; end }<br />m.extend(m)<br />assert_equal m.hello, "world"Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-1455551934161369162011-01-17T15:39:17.896+01:002011-01-17T15:39:17.896+01:00francois:
I ignored the db issue for the sake of ...francois:<br /><br />I ignored the db issue for the sake of simplicity. <br /><br />I would need to pass the db object somehow. With ActiveRecord I need to pass self (that's the problem with inheritance in AR). You're right that in this case it's less valuable.Andrzej Krzywdahttps://www.blogger.com/profile/06399276063142826365noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-40124078238680816892011-01-17T15:26:46.460+01:002011-01-17T15:26:46.460+01:00Given your last example, how is the Buyer class to...Given your last example, how is the Buyer class to know how to store data in the DB, without accepting some kind of User or user_id?<br /><br />If you send along self, how is that better than using a module?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-54698680524658907162011-01-17T15:14:52.484+01:002011-01-17T15:14:52.484+01:00Iain:
Thanks for your comment.
"It's n...Iain: <br /><br />Thanks for your comment.<br /><br />"It's not "everything is a class", but "everything is an object". Modules are objects, not classes."<br /><br />I wasn't very clear with my explanation, what I meant was that you can't easily test a module in isolation because there's no such thing as module instance.<br /><br />Using Postmodern's example, I can't say:<br />m = Module.new { def hello; 'world'; end }<br />assert_equal m.hello, "world"<br /><br />"Ruby's mixin's are very powerful, so it's natural to see a tendency towards that, which leaves composition underused." <br /><br />This is exactly the answer I was looking for. I think you're right that's the reason for composition being so unpopular.<br /><br />I use modules and they help a lot. It's a bit hard to explain but the problem I have with modules is that they create a dependency that's a bit hard to "unwire".<br /><br />I would like to move some logic to a module but when I use the base class in tests I would like to provide a mock, so that the real methods of the module are not executed (for example because they require internet connection or db access). <br />It's a bit hard to do so with modules. Maybe I'm missing something?Andrzej Krzywdahttps://www.blogger.com/profile/06399276063142826365noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-56659594765124828262011-01-17T09:17:19.590+01:002011-01-17T09:17:19.590+01:00Modules/Mixins are actually Ruby's way of doin...Modules/Mixins are actually Ruby's way of doing multiple inheritance, but without the diamond problem. <br /><br />The fact that they cannot be instantiated, doesn't mean it breaks the concept of "everything being an object", since a module is still an object. It's not "everything is a class", but "everything is an object". Modules are objects, not classes. But classes are objects. Still following? It might not fit well with your understanding of OO, but it fit very cleanly in the Ruby Object Model.<br /><br />Thirdly, Rails has support for composition/aggregation, but the syntax is pretty weak. See the method composed_of. I usually go with a DIY approach.<br /><br />I think the conclusion is that every language looks at OO through its own colored glasses. Some concepts of OO are more natural to one language than to the other. Ruby's mixin's are very powerful, so it's natural to see a tendency towards that, which leaves composition underused.Iainhttps://www.blogger.com/profile/11417603787856807208noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-79602251462796939132011-01-17T08:23:43.244+01:002011-01-17T08:23:43.244+01:00You can infact instantiate a Module:
m = Modu...You can infact instantiate a Module:<br /><br /> m = Module.new { def hello; 'world'; end }<br /> m.class # => Module<br /> m.class.class # => Class<br /><br /> include m<br /> hello # => 'world'Postmodernhttp://postmodern.github.com/noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-60563036117473535042011-01-17T04:16:40.246+01:002011-01-17T04:16:40.246+01:00(2) is called 'mixin'(2) is called 'mixin'Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-49723332835524102982011-01-17T04:16:19.949+01:002011-01-17T04:16:19.949+01:00by 'modules' i think you mean 'mixin&#...by 'modules' i think you mean 'mixin'Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-74418012688260106292011-01-17T04:16:09.554+01:002011-01-17T04:16:09.554+01:00by 'modules' i think you mean 'mixin&#...by 'modules' i think you mean 'mixin'Anonymousnoreply@blogger.com