Ruby program design question ( Pattern or AntiPattern ?)

    • 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
    • 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-----

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

In article 51172973522.20020927095543@integ.ru,

···

Bulat Ziganshin 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/

Booker C. Bense 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

[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