------art_31815_30029556.1145765378256
Content-Type: multipart/alternative; 
	boundary---art_31816_14544355.1145765378256"

------art_31816_14544355.1145765378256
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I forgot, here's the test data in case anyone wants it.

Michael Guterl

On 4/23/06, Michael Guterl <mguterl / gmail.com> wrote:
>
> I finally decided to dive in and give metaprogramming in Ruby a shot.  I'm
> not sure that my example is exactly practical, but it seemed useful at the
> time.  There's a few areas I would like to hear others suggestions on:
>
> 1. Does the syntax appear to be in line with the community standards?
>
> 2. Are my uses of class_eval and instance_eval okay / is there a better
> way?
>
> 3. I am using facets because I like the Dictionary (OrderedHash), but I'm
> sure there are better ways to build the structure while preserving the field
> order.  I especially don't like the fact that I have to specify Dictionary[]
> in the structure of the subclass.
>
> 4. Please comment on the code in general, I'm open to any kind of
> criticism.
>
> I'm not sure if it's customary to attach the code in a file or post it in
> the email.  I apologize if I should have attached the code in a file.
>
> Michael Guterl
>
> ----------------------------------------------
>
> require 'rubygems'
> require 'facets'
> require 'dictionary'
>
> class FixedLength
>
>   def self.structure(ordered_hash)
>
>     class_eval do
>       @@structure = ordered_hash
>     end
>
>     keys = @@structure.keys
>     instance_eval do
>       attr_accessor *keys
>     end
>
>   end
>
>   # this entire method could probably be a lot cleaner
>
>   def self.open(file_name)
>     class_eval do
>       data = IO.read(file_name)
>       records = []
>       data.each_line do |line|
>         last_position = 0
>         record = self.new
>         @@structure.each_pair do |name, length|
>           record.instance_variable_set( "@#{name.to_s}", line.slice(last_position,
> length.to_i).strip)
>           last_position += length.to_i
>         end
>         records << record
>       end
>       return records
>     end
>   end
>
> end
>
> class InputStructure < FixedLength
>
>   # I'd like to clean this up, either removing the need for Dictionary[]
> and the separation by commas
>   # or with something like this
>   # column.add :id, 10
>   # column.add :phone, 10
>   # column.add :first_name, 25
>   # etc.  I'd love to hear some suggestions.
>
>   structure Dictionary[ :id , 10,
>             :phone , 10,
>             :first_name , 25, :middle_name , 1, :last_name , 25,
>             :address , 30,
>             :city , 25,
>             :state , 2,
>             :zip , 5, :zip4 , 4 ]
>
> end
>
> require 'test/unit'
>
> class InputStructureTest < Test::Unit::TestCase
>
>   def setup
>     @records = InputStructure.open('fixed_data.txt')
>   end
>
>   def test_first
>     record = @records.first
>     assert_equal "1", record.id
>     assert_equal "1234567890", record.phone
>     assert_equal "SOME RANDOM", record.first_name
>     assert_equal "", record.middle_name
>     assert_equal "PERSON", record.last_name
>     assert_equal "1234 SOME RANDOM STREET", record.address
>     assert_equal "RANDOM CITY", record.city
>     assert_equal "OH", record.state
>     assert_equal "45219", record.zip
>     assert_equal "", record.zip4
>   end
>
> end
>
>

------art_31816_14544355.1145765378256
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I forgot, here's the test data in case anyone wants it.<br><br>Michael Guterl<br><br><div><span class="gmail_quote">On 4/23/06, <b class="gmail_sendername">Michael Guterl</b> &lt;<a href="mailto:mguterl / gmail.com">mguterl / gmail.com
</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div style="direction: ltr;">I finally decided to dive in and give metaprogramming in Ruby a shot.&nbsp; I'm not sure that my example is exactly practical, but it seemed useful at the time.&nbsp; There's a few areas I would like to hear others suggestions on:
<br>
<br>1. Does the syntax appear to be in line with the community standards?<br><br>2. Are my uses of class_eval and instance_eval okay / is there a better way?<br><br>3. I am using facets because I like the Dictionary (OrderedHash), but I'm sure there are better ways to build the structure while preserving the field order.&nbsp; I especially don't like the fact that I have to specify Dictionary[] in the structure of the subclass.
<br><br>4. Please comment on the code in general, I'm open to any kind of criticism.<br><br>I'm not sure if it's customary to attach the code in a file or post it in the email.&nbsp; I apologize if I should have attached the code in a file.
<br><br>Michael Guterl<br><br>----------------------------------------------<br><br>require 'rubygems'<br>require 'facets'<br>require 'dictionary'<br><br>class FixedLength<br><br>&nbsp; def self.structure(ordered_hash)<br>&nbsp; <br>

&nbsp;&nbsp;&nbsp; class_eval do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @@structure = ordered_hash<br>&nbsp;&nbsp;&nbsp; end<br>&nbsp; <br>&nbsp;&nbsp;&nbsp; keys = @@structure.keys<br>&nbsp;&nbsp;&nbsp; instance_eval do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; attr_accessor *keys<br>&nbsp;&nbsp;&nbsp; end<br>&nbsp;&nbsp;&nbsp; <br>&nbsp; end<br>&nbsp; <br>&nbsp; # this entire method could probably be a lot cleaner
<br><br>&nbsp; def self.open(file_name)<br>&nbsp;&nbsp;&nbsp; class_eval do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data = IO.read(file_name)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; records = []<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data.each_line do |line|<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; last_position = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; record = self.new<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @@structure.each_pair

 do |name, length|<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; record.instance_variable_set( &quot;@#{name.to_s}&quot;, line.slice(last_position, length.to_i).strip)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; last_position += length.to_i<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; records &lt;&lt; record
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return records<br>&nbsp;&nbsp;&nbsp; end<br>&nbsp; end<br><br>end<br><br>class InputStructure &lt; FixedLength<br><br>&nbsp; # I'd like to clean this up, either removing the need for Dictionary[] and the separation by commas
<br>&nbsp; # or with something like this<br>&nbsp; # column.add :id, 10<br>&nbsp; # column.add :phone, 10<br>&nbsp; # column.add :first_name, 25<br>&nbsp; # etc.&nbsp; I'd love to hear some suggestions.<br><br>&nbsp; structure Dictionary[ :id , 10, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :phone , 10, 
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :first_name , 25, :middle_name , 1, :last_name , 25, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :address , 30, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :city , 25, <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :state , 2,br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :zip , 5, :zip4 , 4 ]<br><br>end<br><br>require 'test/unit'
<br><br>class InputStructureTest &lt; Test::Unit::TestCase<br><br>&nbsp; def setup<br>&nbsp;&nbsp;&nbsp; @records = InputStructure.open('fixed_data.txt')<br>&nbsp; end<br>&nbsp; <br>&nbsp; def test_first<br>&nbsp;&nbsp;&nbsp; record = @records.first<br>&nbsp;&nbsp;&nbsp; assert_equal &quot;1&quot;, 
<a href="http://record.id" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">record.id</a><br>&nbsp;&nbsp;&nbsp; assert_equal &quot;1234567890&quot;, record.phone<br>&nbsp;&nbsp;&nbsp; assert_equal &quot;SOME RANDOM&quot;, record.first_name
<br>&nbsp;&nbsp;&nbsp; assert_equal &quot;&quot;, record.middle_name<br>&nbsp;&nbsp;&nbsp; assert_equal &quot;PERSON&quot;, 
record.last_name<br>&nbsp;&nbsp;&nbsp; assert_equal &quot;1234 SOME RANDOM STREET&quot;, record.address<br>&nbsp;&nbsp;&nbsp; assert_equal &quot;RANDOM CITY&quot;, record.city<br>&nbsp;&nbsp;&nbsp; assert_equal &quot;OH&quot;, record.state<br>&nbsp;&nbsp;&nbsp; assert_equal &quot;45219&quot;, 
record.zip<br>&nbsp;&nbsp;&nbsp; assert_equal &quot;&quot;, record.zip4<br>&nbsp; end<br>&nbsp; <br>end<br><br>

</div></blockquote></div><br>

------art_31816_14544355.1145765378256--

------art_31815_30029556.1145765378256
Content-Type: text/plain; name=fixed_data.txt; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Attachment-Id: f_emcv3dvs
Content-Disposition: attachment; filename="fixed_data.txt"

1         1234567890SOME RANDOM               PERSON                   1234 SOME RANDOM STREET       RANDOM CITY              OH45219    

------art_31815_30029556.1145765378256--