# Noob: Review code for us of object orientation

**URL:** <https://www.ruby-forum.com/t/noob-review-code-for-us-of-object-orientation/183566>\
**Category:** Ruby\
**Created:** [February 16, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/noob-review-code-for-us-of-object-orientation/183566 "2010-02-16T17:58:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lsumnler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/35a633/32.png) [@lsumnler](https://www.ruby-forum.com/u/lsumnler)\
**Post date:** [February 16, 2010, 5:58pm UTC](https://www.ruby-forum.com/t/noob-review-code-for-us-of-object-orientation/183566/1 "2010-02-16T17:58:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lsumnler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/35a633/32.png) [@lsumnler](https://www.ruby-forum.com/u/lsumnler)\
**Post date:** [February 16, 2010, 10:55pm UTC](https://www.ruby-forum.com/t/noob-review-code-for-us-of-object-orientation/183566/2 "2010-02-16T22:55:32Z")

</div>

On 02/16/2010 05:58 PM, Len S. wrote:

> 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
