跳至主要内容

Seven Languages in Seven Weeks

This morning, after 10 minutes struggle of do or do not follow the self study section. Just spend several hours of searching, coding and debugging, finally I got the following ruby script that is able to pass the self study excise.
diff -r 9efeca9bd507 learning/ruby/acts_as_csv_module.rb
--- a/learning/ruby/acts_as_csv_module.rb   Tue Feb 07 15:13:11 2017 +0800
+++ b/learning/ruby/acts_as_csv_module.rb   Tue Feb 07 15:13:49 2017 +0800
@@ -1,32 +1,90 @@
 module ActsAsCsv
+
   def self.included(base)
     base.extend ClassMethods
   end
+
   module ClassMethods
+    
     def acts_as_csv
       include InstanceMethods
     end
+
+    def acts_as_enumerable
+      include Enumerable
+    end
   end
+
   module InstanceMethods
+    
     def read
       @csv_contents = []
       filename = self.class.to_s.downcase + '.txt'
       file = File.new(filename)
-      @headers = file.gets.chomp.split(', ' )
+      CsvRow.headers = file.gets.chomp.split(', ' )
+
+      # puts "CsvRow.headers is #{CsvRow.headers}"
       file.each do |row|
-        @csv_contents << row.chomp.split(', ' )
+        csvrow = CsvRow.new
+        csvrow.row_content = row.chomp.split(', ' )
+
+        # puts "csvrow is #{csvrow.row_content}"
+   @csv_contents << csvrow
       end
     end
-    attr_accessor :headers, :csv_contents
+    
+    attr_accessor :csv_contents
+    
     def initialize
       read
     end
+
+    def each(&block)
+      # puts "csv_contents.each is #{@csv_contents.each}"
+      @csv_contents.each(&block)
+    end
+    
   end
 end
+
 class RubyCsv # no inheritance! You can mix it in
   include ActsAsCsv
   acts_as_csv
+  acts_as_enumerable
 end
-m = RubyCsv.new
-puts m.headers.inspect
-puts m.csv_contents.inspect
+
+class CsvRow
+  @@header_indexes = {}
+  
+  def method_missing name, *args
+    @row_content[@@header_indexes[name.to_s]]
+  end
+
+  attr_accessor :row_content
+
+  def initialize
+    @row_content = []
+  end
+
+  def self.headers=(h)
+    @@headers = h
+
+    # puts "h is #{h}"
+    h.each_index {|i| @@header_indexes[h[i]] = i}
+    # puts "@@header_indexes is #{@@header_indexes}"
+  end
+
+  def self.headers
+    @@headers
+  end
+end
+
+
+# m = RubyCsv.new
+# puts m.headers.inspect
+# puts m.csv_contents.inspect
+
+csv = RubyCsv.new
+
+# puts "csv_contents is #{csv.csv_contents}"
+csv.each {|row| puts row.one}

Ruby feature learned

  1. Class variable is defined and accessed with “double at” prefix @@.
  2. Class accessor must be defined with self. prefix, otherwise the result method will be instance accessor, I was confused by the complex syntax sugar used in the link.
  3. Array has convenient .each_index method that will save you lots of type.
  4. each method have to be defined as each(&block) to be able to work, and the inside each being invoked should also be invoked as .each(&block) for the block to be passed into it.
  5. attr_accessor will generate ruby specific getter and setters codes for decorated attributes(have to be specified in the form of :rubysymbol).

 

评论

此博客中的热门博文

Use MobaDiff with git difftool

Recently there's an activity in IT that forces the deletion of all unauthorized softwares from all work machines. Unfortunately, kdiff3 is one in the list. As it is generally okay to use vimdiff as an alternative for kdiff3, A gui tool is better suited for desktop workflows. Known that MobaXterm is shipping a gui diff tool named MobaDiff. But it only appears in the windows right click context menu. Find the real application name takes me some time to search in the windows registry. "MobaRTE.exe", which is the one invoked by HKCR\*\shell\MobaDiff. And it was invoked with "-contextdiff" switch to show MobaDiff UI, while when the switch is "-contextedit" it shows MobaTextEditor. Too bad that the "-contextdiff" switch do not support pre-image post-image as other diff tool did, which effectively made it unable to be used as a command line diff utility. Also MobaTech did not mention anything in their document of this Mob...

Emacs 26.2 windows Hunspell setup

 Hunspell failed with default setup With my shining new emacs26.2 setup, I am happy until trying to `M-$’ on a word. I got following error message, it used to be work by just set ispell program to hunspell. Starting new Ispell process hunspell with default dictionary... split-string: Wrong type argument: stringp, nil After some check in my customization setting. nothing found. install a copy of hunspell dictionary At first I think it was caused by no hunspell dictionary. I downloaded en US and en CA hunspell dictionary from wordlist.sf.net project. After put the .aff and .dic files into C:directory I’ve also made a symbolic link from en US ones by running the `mklink’ tool: mklink default.aff en_US.aff mklink default.dic en_US.dic Err… hunspell does not work, still. Time to read code Dig into ispell.el I suprisingly find there’re lots of hunspell specific logic. Originally I was thinking hunspell support involves no elisp work. Acturally ispell did many work to de...

XEmacs 21.5 beta 35 "kohlrabi" has been released.

If you are an old XEmacs user, you may feel happy to see this from https://www.xemacs.org/.    After ten years, XEmacs released a new version 21.5. So there's still many people cares about XEmacs. The XEmacs' source repo have been moved from altassian Bitbucket to https://heptapod.net/. As Bitbucket have been dropped Mercurial support many years ago.