Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.98 KB | None | 0 0
  1. # class Tree
  2.     # attr_accessor :children, :node_name
  3.    
  4.     # def initialize(hash)
  5.         # @children = []
  6.         # hash.keys.each do |key|
  7.             # @node_name = key
  8.             # hash[key].keys.each do |childkey|
  9.                 # @children << Tree.new({ childkey => hash[key][childkey] })
  10.             # end
  11.         # end
  12.     # end
  13.  
  14.     # def visit_all(&block)
  15.         # visit &block
  16.         # children.each {|c| c.visit_all &block}
  17.     # end
  18.    
  19.     # def visit(&block)
  20.         # block.call self
  21.     # end
  22.    
  23.     # def to_s
  24.         # puts @node_name
  25.     # end
  26. # end
  27.  
  28. # x = {"grandpa" => { "dad" => {"child 1" => {}, "child 2" => {} }, "uncle" => {"child 3" => {}, "child 4" => {} } } }
  29. # y = Tree.new(x)
  30. # y.visit_all { |c| c.to_s }
  31. # i = 0
  32. # File.open("test.txt").each { |line| puts i.to_s + " | " + line if line =~ /blue/ ; i = 1 + i }
  33.  
  34. module ActsAsCsv
  35.     #where should this go?
  36.     def self.method_missing name, *args
  37.         i = @headers.index(name)
  38.        
  39.         return if i.nil?
  40.        
  41.         @csv_contents.each do |c|
  42.             puts c[i]
  43.         end
  44.     end
  45.    
  46.     def self.included(base)
  47.         base.extend ClassMethods
  48.     end
  49.    
  50.     def self.method_missing name, *args
  51.         i = @headers.index(name)
  52.        
  53.         return if i.nil?
  54.        
  55.         @csv_contents.each do |c|
  56.             puts c[i]
  57.         end
  58.     end
  59.    
  60.     module ClassMethods
  61.         def acts_as_csv
  62.             include InstanceMethods
  63.         end
  64.     end
  65.    
  66.     module InstanceMethods
  67.         def read
  68.             @csv_contents = []
  69.             filename = self.class.to_s.downcase + '.txt'
  70.             file = File.new(filename)
  71.             @headers = file.gets.chomp.split(', ')
  72.             file.each do |row|
  73.                 @csv_contents << row.chomp.split(', ')
  74.             end
  75.         end
  76.    
  77.         attr_accessor :headers, :csv_contents
  78.        
  79.         def initialize
  80.             read
  81.         end    
  82.     end
  83. end
  84.  
  85. class RubyCsv # no inheritance! You can mix it in
  86.     include ActsAsCsv
  87.     acts_as_csv
  88. end
  89.  
  90. m = RubyCsv.new
  91. puts m.headers.inspect
  92. puts m.csv_contents.inspect
  93. m.one
  94.  
  95. # OUTPUT:
  96. #["one", "two"]
  97. #[["1correct", "1wrong"], ["2correct", "2wrong"], ["3correct", "3wrong"], ["4correct", "4wrong"], ["5correct", "5wrong"]]
  98. #./grep.rb:93: undefined method `one' for #<RubyCsv:0x3fd3e18> (NoMethodError)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement