# Ruby program design question ( Pattern or AntiPattern ?)

**URL:** <https://rubytalk.org/t/ruby-program-design-question-pattern-or-antipattern/2004>\
**Category:** ruby-talk\
**Created:** [26 September 2002 19:36 UTC](https://rubytalk.org/t/ruby-program-design-question-pattern-or-antipattern/2004 "2002-09-26T19:36:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bbense\_Comp\_Lang\_R57](https://avatars.discourse-cdn.com/v4/letter/b/7bcc69/32.png) [@Bbense\_Comp\_Lang\_R57](https://rubytalk.org/u/Bbense_Comp_Lang_R57)\
**Post date:** [26 September 2002 19:36 UTC](https://rubytalk.org/t/ruby-program-design-question-pattern-or-antipattern/2004/1 "2002-09-26T19:36:24Z")

</div>

- 
  - I find myself repeating this kind of pattern over and over  
again. I’m wondering if it’s a good sign or a bad one. Here’s  
the pattern.

Write a simple generic ruby class to interface to something.

In a specific program, write a Wrapper class that essentially  
has the same methods as the simple class, but includes a lot  
of program specific cruft… More specifically, it does not  
subclass the original class.

- 
  - I think this is good in that I get to reuse the simple class  
over and over again, but I’m wondering if I’m just not making  
my simple classes generic enough. This pattern fits my design  
style of “bottom-up” programming, but I can see it getting  
very redundant once you get beyond a certain level of  
complexity. It suggests to me that I need to rexamine something  
but I can’t figure out what to start looking at.

- 
  - Booker C. Bense

---

<div class="post-metadata">

**Author:** ![MikkelFJ1](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@MikkelFJ1](https://rubytalk.org/u/MikkelFJ1)\
**Post date:** [26 September 2002 20:16 UTC](https://rubytalk.org/t/ruby-program-design-question-pattern-or-antipattern/2004/2 "2002-09-26T20:16:45Z")

</div>

> - 
> - I think this is good in that I get to reuse the simple class  
> over and over again, but I’m wondering if I’m just not making  
> my simple classes generic enough. This pattern fits my design  
> style of “bottom-up” programming, but I can see it getting  
> very redundant once you get beyond a certain level of  
> complexity. It suggests to me that I need to rexamine something  
> but I can’t figure out what to start looking at.

You should consider using mixins.

You also seem to repeat the pattern below…

Mikkel

> **···**
>
> > -----BEGIN PGP SIGNATURE-----  
> > Version: 2.6.2
> > 
> > iQCVAwUBPZNeYGTWTAjn5N/lAQHaXQQAgTcZ05+njSKazKaWaJ7drWYfOsdgddzo  
> > 3qLamlzELVRSURhBF3BTZoZJdICjEyoi4FeCHKrYfzuGJuZGT4CZr7FsSkN/WPCi  
> > ERz38gnEK2hroR78mMitVYiH/I85bFUcPOIZ6h64GZ1MmC4f/BpicUrwdu6+ZBMK  
> > ZNCsvmWDd3Q=  
> > =wu2t  
> > -----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Bulat\_Ziganshin](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Bulat\_Ziganshin](https://rubytalk.org/u/Bulat_Ziganshin)\
**Post date:** [27 September 2002 05:56 UTC](https://rubytalk.org/t/ruby-program-design-question-pattern-or-antipattern/2004/3 "2002-09-27T05:56:52Z")

</div>

Hello bbense+comp,

Thursday, September 26, 2002, 11:36:24 PM, you wrote:

bclrS20tse\> - - I find myself repeating this kind of pattern over and over  
bclrS20tse\> again. I’m wondering if it’s a good sign or a bad one. Here’s  
bclrS20tse\> the pattern.

bclrS20tse\> Write a simple generic ruby class to interface to something.

delegation pattern? please provide code example

> **···**
>
> –  
> Best regards,  
> Bulat [mailto:bulatz@integ.ru](mailto:bulatz@integ.ru)

---

<div class="post-metadata">

**Author:** ![Bbense\_Comp\_Lang\_R58](https://avatars.discourse-cdn.com/v4/letter/b/5f8ce5/32.png) [@Bbense\_Comp\_Lang\_R58](https://rubytalk.org/u/Bbense_Comp_Lang_R58)\
**Post date:** [27 September 2002 18:03 UTC](https://rubytalk.org/t/ruby-program-design-question-pattern-or-antipattern/2004/4 "2002-09-27T18:03:32Z")

</div>

In article [51172973522.20020927095543@integ.ru](mailto:51172973522.20020927095543@integ.ru),

> **···**
>
> Bulat Ziganshin [bulatz@integ.ru](mailto:bulatz@integ.ru) wrote:
> 
> > Hello bbense+comp,
> > 
> > Thursday, September 26, 2002, 11:36:24 PM, you wrote:
> > 
> > bclrS20tse\> - - I find myself repeating this kind of pattern over and over  
> > bclrS20tse\> again. I’m wondering if it’s a good sign or a bad one. Here’s  
> > bclrS20tse\> the pattern.
> > 
> > bclrS20tse\> Write a simple generic ruby class to interface to something.
> > 
> > delegation pattern? please provide code example
> 
> - 
> - Here’s a trivial example. I’ve been using ifile to help sort  
> spam in my ruby mailhandler. Here’s the ifile class
> 
> # 
> 
> # $Id: ifile.rb,v 1.1 2002/09/25 20:02:13 bbense Exp $
> 
> # 
> 
> # Ifile, a class for interacting with ifile program.
> 
> # Get ifile at [http://www.ai.mit.edu/~jrennie/ifile/](http://www.ai.mit.edu/~jrennie/ifile/)
> 
> # 
> 
> # Booker C. Bense [bbense@slac.stanford.edu](mailto:bbense@slac.stanford.edu)
> 
> # 
> 
> require ‘open3’
> 
> module Ifile
> 
> # This is the wrong name, but I can’t think of anything better.
> 
> class Process
> 
> # Tell me where ifile lives.
> 
> def initialize(path=“/var/local/bin/ifile”,args=“–verbosity=0”)  
> if FileTest.executable?(path) then  
> @ifile = path  
> @args = args  
> else  
> raise ArgumentError  
> end  
> end
> 
> # Given a message, query folders
> 
> def query(msg)  
> results = Array.new  
> output = self.run\_ifile(msg,"–query ")  
> i = 0  
> output.each do |line|  
> # Format of output is folder score  
> folder , score = line.split  
> if ( folder && score ) then  
> tmp = Hash.new  
> tmp[‘folder’] = folder  
> tmp[‘score’] = score.to\_f  
> tmp[‘position’] = i  
> results \<\< tmp  
> i = i + 1  
> end  
> end  
> return results  
> end
> 
> # Add a message to a folder
> 
> def add(msg,folder)  
> output = self.run\_ifile(msg,“–insert=#{folder}”)  
> end
> 
> # Delete a message from a folder
> 
> def delete(msg,folder)  
> output = self.run\_ifile(msg,“–delete=#{folder}”)  
> end
> 
> # Refile
> 
> def refile(msg,oldfolder,newfolder)  
> self.delete(msg,oldfolder)  
> self.add(msg,newfolder)  
> end
> 
> # internal methods
> 
> def run\_ifile(msg,args)  
> stdin, stdout, stderr = Open3.popen3(“#{@ifile} #{@args} #{args}”)  
> #write msg to ifile  
> msg.each { |line| stdin.puts line }  
> stdin.close  
> #Read output  
> output = stdout.readlines  
> stdout.close  
> stderr.close  
> return output  
> end
> 
> end
> 
> end # module Ifile
> 
> - 
> - I used this module in several programs, sometimes  
> I can use it without a “controller” , however I tend  
> to end up writing stuff like this.
> 
> # For dealing with ifile
> 
> class IfileProcess
> 
> def initialize(filterdir,default\_folder)  
> @filterdir = filterdir  
> @default\_folder = default\_folder
> 
> ```
> @ifile = Ifile::Process.new
> @same = Hash.new
> @same["unix-admin"] = "oldmail"
> @same["admin-log"] = "oldmail"
> @same[default_folder] = "oldmail"
> 
> ```
> 
> end
> 
> # Return a full fledge path
> 
> def query(msg)  
> results = @ifile.query(msg)  
> best = results[0][“folder”]  
> if ( @same[best] ) then  
> return @default\_folder  
> end  
> dest = “#{@filterdir}/#{best}”  
> dest.sub!(/\s/,“”)  
> return dest  
> end
> 
> def add(msg,destination)  
> if ( @same[destination] ) then  
> folder = @same[destination]  
> else  
> folder = destination.sub(@filterdir,“”)  
> folder.sub!(/[/]/,“”)  
> # Deal with incoming/oldmail problem  
> if ( @same[folder] )  
> folder = @same[folder]  
> end  
> end  
> print “Adding message to :#{folder}:\n”  
> @ifile.add(msg,folder)  
> end
> 
> end #IfileProcess
> 
> - 
> - As you can see IfileProcess has a bunch of icky  
> knowledge about other classes and default paths  
> that I have kept out of the simple class. Now that  
> I think about it I could probably just as easily  
> implement these as subclasses of the original  
> class. It never occurs to me to do that however.
> 
> - 
> - Booker C. Bense

---

<div class="post-metadata">

**Author:** ![Mauricio\_Fernndez](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mauricio\_Fernndez](https://rubytalk.org/u/Mauricio_Fernndez)\
**Post date:** [27 September 2002 19:13 UTC](https://rubytalk.org/t/ruby-program-design-question-pattern-or-antipattern/2004/5 "2002-09-27T19:13:35Z")

</div>

[deleted]

> - 
> - As you can see IfileProcess has a bunch of icky  
> knowledge about other classes and default paths  
> that I have kept out of the simple class. Now that  
> I think about it I could probably just as easily  
> implement these as subclasses of the original  
> class. It never occurs to me to do that however.

This looks to me like implementation inheritance.

> **···**
>
> On Sat, Sep 28, 2002 at 03:03:32AM +0900, bbense+comp.lang.ruby.Sep.27.02@telemark.stanford.edu wrote:
> 
> –  
> \_ \_
> 
> > > \_\_ \_\_ _| |_ \_\_\_ \_ \_\_ \_\_\_ \_\_ \_ \_ \_\_  
> > > '\_ \ / _`| __/__ | '_` \_ \ / ` | ’ \   
> > > ) | (| | |_\_\_ \ | | | | | (_| | | | |  
> > > .\_\_/ \_,_|\ __|__ /| |_| |_|\__,_|_| |\_|  
> > > Running Debian GNU/Linux Sid (unstable)  
> > > batsman dot geo at yahoo dot com
> 
> Are we going to make an emacs out of apt?  
> APT - Debian in a program. It even does your laundry  
> – Seen on #Debian
