tag:blogger.com,1999:blog-22587889.post8843512088672414635..comments2024-02-11T13:21:47.930+05:30Comments on Ruminations of a Programmer: Why Thinking in Ruby MattersAnonymoushttp://www.blogger.com/profile/01613713587074301135noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-22587889.post-32923887347495515102007-06-19T13:21:00.000+05:302007-06-19T13:21:00.000+05:30[there should be a great way to achieve this combi...<I>[there should be a great way to achieve this combining dynamic mixins]</I><BR/>Sure .. Ruby gives you multiple options of implementation for almost everything. Here is one with dynamic mixin :<BR/><BR/>class SalaryCompute<BR/> def self.strategy *props<BR/> props.each do |prop|<BR/> self.class_eval <<-EOS<BR/> include #{prop}<BR/> EOS<BR/> end<BR/> end<BR/> <BR/> strategy :DefaultStrategy<BR/> <BR/> def initialize basic<BR/> @basic = basic<BR/> @hra = basic * 0.5<BR/> end<BR/> <BR/> def compute<BR/> do_compute(@basic, @hra)<BR/> end<BR/>end<BR/><BR/>puts SalaryCompute.new(100).compute<BR/><BR/>And, in fact you can take the strategy method out of this class and have it somewhere as as a dynamic strategy method.Anonymoushttps://www.blogger.com/profile/01613713587074301135noreply@blogger.comtag:blogger.com,1999:blog-22587889.post-19352602007218282202007-06-12T22:57:00.000+05:302007-06-12T22:57:00.000+05:30Really, there should be a great way to achieve thi...Really, there should be a great way to achieve this combining dynamic mixins (i.e., mixins that are done at runtime) and some instance data. So you'll maybe instantiate your object with an @strategy or @type instance variable (or, in the case of a database, provide it), and that will trigger something like an instance-level include (using class << self) that includes the appropriate strategy. Add a simple strategy= method and encapsulate it all in one place.Antoniohttps://www.blogger.com/profile/00685775039560712665noreply@blogger.com