Noob: Review code for us of object orientation

Discussion in 'Ruby' started by Len Sumnler, Feb 16, 2010.

  1. Len Sumnler

    Len Sumnler Guest

    Would appreciate is if someone could look over the attached file which
    contains two modules.

    I have a large number of old Cobol files to convert into a MySQL
    database. The programs work upto this point and I have not added the
    code to move the result into MySQL yet. It merely prints the hash of
    the binary file so far.

    I am just starting to learn Ruby and OOP as well and am more concerned
    that I am bringing to much of my procedural coding style into my OOP
    programs.

    Comments, suggestions and advice welcomed.

    Len

    Attachments:
    http://www.ruby-forum.com/attachment/4489/filepost.txt

    --
    Posted via http://www.ruby-forum.com/.
     
    Len Sumnler, Feb 16, 2010
    #1
    1. Advertising

  2. On 02/16/2010 05:58 PM, Len Sumnler wrote:
    > Would appreciate is if someone could look over the attached file which
    > contains two modules.
    >
    > I have a large number of old Cobol files to convert into a MySQL
    > database. The programs work upto this point and I have not added the
    > code to move the result into MySQL yet. It merely prints the hash of
    > the binary file so far.
    >
    > I am just starting to learn Ruby and OOP as well and am more concerned
    > that I am bringing to much of my procedural coding style into my OOP
    > programs.
    >
    > Comments, suggestions and advice welcomed.


    I believe it looks pretty good already. I think you have grokked OO
    basics, although you could have more classes - something which often
    happens when starting with OO. In your case I would add these classes

    RecordField = Struct.new :name, :type, :length, :decimal

    aropnfil = [
    RecordField['cust_no', :Xstr:, 6, 0],
    RecordField['doc_date', :Date, 6, 0],
    ....
    ]

    Also I'd use Symbols rather than String for the type qualifiers (or even
    class objects, if that's possible) because they are more efficient than
    String (you typically have only few values and each repeats quite a bit).

    Your record_length is redundant if I'm not mistaken (it's the sum of all
    field lengths). So I'd probably also add a class for the complete
    record description.

    RecordDefinition = Struct.new :record_fields do
    def initialize(fields = [])
    self.record_fields = fields
    end

    def record_length
    record_fields.inject(0) {|sum, rd| sum + rd.length}
    end
    end


    aropnfil = RecordDefinition.new [
    RecordField['cust_no', :Xstr:, 6, 0],
    RecordField['doc_date', :Date, 6, 0],
    ....
    ]


    I would then model WangFile's interface similar to File and include
    Enumerable

    def WangFile

    def self.foreach(*a, &b)
    wf = open(*a)
    begin
    wf.each_record(&b)
    ensure
    wf.close
    end
    end

    def self.open(*a)
    new(*a)
    end

    def close
    ...
    end

    def initialize(file_name, record_definition)
    ...
    end

    include Enumerable

    def each_record
    len = record_definition.record_length

    while buffer = ....read(len)
    yield(parse_record(buffer))
    end

    self
    end

    alias each each_record
    end

    And a minor detail: you can save typing by doing

    hash_record[name] = case type
    when :Xstr
    @record[start,len]
    when :Xint
    @record...

    Kind regards

    robert


    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Feb 16, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tommy
    Replies:
    2
    Views:
    360
    John C. Bollinger
    Nov 22, 2004
  2. Mike Wahler
    Replies:
    1
    Views:
    378
    Az Tech
    Aug 2, 2003
  3. Vishal Naidu

    Object orientation.....

    Vishal Naidu, Sep 1, 2005, in forum: C Programming
    Replies:
    11
    Views:
    623
    Alexei A. Frounze
    Sep 28, 2005
  4. www
    Replies:
    51
    Views:
    1,501
  5. Brian A.
    Replies:
    4
    Views:
    140
    Martin DeMello
    Nov 28, 2008
Loading...

Share This Page