From: Robert Klemme Date: 2010-09-14T18:26:49+09:00 Subject: Re: Converting from one class to another in Ruby On Tue, Sep 14, 2010 at 4:35 AM, Harry Spier wrote: > Many thanks for your reply. You're welcome. > I have simplified somewhat.  Its an OCR application where Binary_matrix > is a black and white pixel representation of a page of text(or line of > text, or letter or part of a letter etc.).  There are many different > objects which are to be converted into Binary_matrix such as the above > portions of an existing Binary_matrix object, or Rmagick ImageList > objects, or graphic objects processed by other packages than RMagick. > This list will definitely grow in the future so for that reason I wanted > the conversion to Binary_matrix logic to reside in the class being > converted to Binary_matrix not in the Binary_matrix class. That's perfectly OK. Just the way you coded your logic is weird IMHO. Just do this for your classes: class A def initialize( what goes here? ) # what do you want to do here? end def to_binary_matrix bm = BinaryMatrix.new # fill bm bm end end > I.e. I didn't want a list of statements in Binary_matrix initialize > method like: > if arg.class == ImageList then ImageList_conversion end > if arg.class == JPEG then jpeg_conversion end > etc. etc. I would not want it that way either nor did I suggest this. > Rather I wanted my Binary_matrix object to check if the object to be > converted to a Binary_matrix had a to_Binary_matrix method and then to > call it. Just call it (see below). > I also require all the Array methods for Binary_matrix because of the > complex traversing and manipulation of the pixel data.  To give a > trivial example: does a black pixel in line n touch a black pixel in > line n+1? What are the array ranges of a contiguous group of black > pixels etc. Change a contiguous range of pixels to white (i.e. clear up > noise) etc. These are the exact operations that I would place in a class BinaryMatrix (btw. note the naming conventions for Ruby constants). An Array is just not a matrix. By exposing all Array methods client code (i.e. classes that use BinaryMatrix) needs to know about the internal representation of a BinaryMatrix which is about the worst thing you can do in an OO application from a design perspective. > Again because of complexity of the application I wanted to make the code > as humanly readable as possible.  So I want to be able to say > page_in_pixels = page_image.to_Binary_matrix > as well as > page_in_pixels = Binary_matrix(page_image) For that to work you only need def BinaryMatrix(x) x.to_binary_matrix end and implement #to_binary_matrix in all your classes properly. > (I'm not saying its necessarily a good solution and I really wish I > could make it less clunky. if there was some way to tell whether method > to_Binary_matrix was being called within the context of Binary_matrix > initialize method or outside it then the flag -from_initialize_method- > wouldn't be necessary) Why and what do you want to convert to BM in #initialize? This hasn't become clear yet. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/