Seven Languages in Seven Weeks: Ruby Day 3
The third and final day of Ruby. This was full of fun, we learned about the power of "missing_method", and how to use it to create your own (DSL). We also learn how create and use modules, modules are pretty cool I liked how you can create a module and mix it in a class. Now on to the homework, todays homework was quite a challenge but also fun.
HW Day 3:
Modify the CSV application to support an each method to return a CsvRow object. Use method_missing on the CsvRow to return the value for the column for a given heading.
Here is the original class
2
3 def self.acts_as_csv
4
5 define_method 'read' do
6 file = File.new(self.class.to_s.downcase + '.txt')
7 @headers = file.gets.chomp.split(', ')
8
9 file.each do |row|
10 @result << row.chomp.split(', ')
11 end
12
13 end
14
15 define_method "headers" do
16 @headers
17 end
18
19 define_method "csv_contents" do
20 @result
21 end
22
23 define_method "initialize" do
24 @result = []
25 read
26 end
27
28 end
29
30end
31class RubyCsv < ActsAsCsv
32end
OK here is what I cam up with.
2class Utilites
3 #Here we are just getting the index of
4 #specific element in an Array
5 def getArrayIndexByElement ( arr, elm )
6
7 #I am using the .to_a just to
8 #ensure that the arr var is an Array
9 #and I am doing the same on elm var
10 #just making sure it's a string
11 return arr.to_a.index( elm.to_s )
12 end
13
14 def getArrayElementByIndex ( arr, idx )
15
16 #I am using the .to_a just to
17 #ensure that the arr var is an Array
18 #and I am doing the same on idx var
19 #just making sure it's a integer
20 return arr.to_a[ idx.to_i]
21 end
22
23end
24#create a new class that acts like a row
25class ActsAsRow
26
27 def initialize ( rowdata, headers )
28 @Utilites = Utilites.new
29 #set class variables
30 #I am using the .to_a to make sure
31 #the vars are Array's
32 @rowdata=rowdata.to_a
33 @headers=headers.to_a
34 end
35
36 def method_missing name, *args
37
38 #First find the index of the header
39 idx = @Utilites.getArrayIndexByElement( @headers.to_a, name.to_s.downcase )
40 #Next get the element in the row
41 #data using the idx var
42 str = @Utilites.getArrayElementByIndex( @rowdata.to_a, idx.to_i )
43 if idx != nil
44 return str.to_s
45 else
46 return "Method Not Found!"
47 end
48
49 end
50end
51class ActsAsCsv
52
53 def self.acts_as_csv
54
55 define_method 'read' do
56 file = File.new(self.class.to_s.downcase + '.txt')
57
58 #create the header array
59 @headers = []
60
61 #loop through the first line in the file
62 #and make them the headers
63 file.gets.chomp.split(', ').each do |t_f|
64
65 #add to the headers Array
66 @headers.push( t_f.to_s.downcase )
67 end
68
69
70 file.each do |row|
71 @result << row.chomp.split(', ')
72 end
73
74 end
75
76 define_method "headers" do
77 @headers
78 end
79
80 define_method "csv_contents" do
81 @result
82 end
83
84 define_method "initialize" do
85 @result = []
86 read
87 end
88
89 end
90
91 def each(&block)
92
93 #loop through all of the rows
94 @result.each do |rowdata|
95 block.call(
96 #create a new row
97 row = ActsAsRow.new( rowdata ,self.headers )
98 );
99 end
100 end
101
102end
103class RubyCsv < ActsAsCsv
104 acts_as_csv
105end
106m = RubyCsv.new
107m.each {|row| puts row.one}
108Output
To sum it all up I would have to say I like Ruby and I might just write a small Ruby app and Maybe check out Ruby on Rails. Well "Hi ho, Io" print its off to learn Io.

Loading...