annotate 1-Ruby/acts_as_csv_module-irb.output @ 51:178b18b4f9ba

Start experimenting with Prolog
author IBBoard <dev@ibboard.co.uk>
date Mon, 25 Sep 2017 20:49:52 +0100
parents 2ea3b5e9b41b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 acts_as_csv_module.rb(main):001:0> #---
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 acts_as_csv_module.rb(main):002:0* # Excerpted from "Seven Languages in Seven Weeks",
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 acts_as_csv_module.rb(main):003:0* # published by The Pragmatic Bookshelf.
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 acts_as_csv_module.rb(main):004:0* # Copyrights apply to this code. It may not be used to create training material,
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 acts_as_csv_module.rb(main):005:0* # courses, books, articles, and the like. Contact us if you are in doubt.
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 acts_as_csv_module.rb(main):006:0* # We make no guarantees that this code is fit for any purpose.
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 acts_as_csv_module.rb(main):007:0* # Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information.
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 acts_as_csv_module.rb(main):008:0* #---
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 acts_as_csv_module.rb(main):009:0* module ActsAsCsv
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 acts_as_csv_module.rb(main):010:1> def self.included(base)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 acts_as_csv_module.rb(main):011:2> base.extend ClassMethods
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 acts_as_csv_module.rb(main):012:2> # The book doesn't do this, but it is possible to add the methods
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 acts_as_csv_module.rb(main):013:2* # without having the inheriting class call a second function to do it
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 acts_as_csv_module.rb(main):014:2* base.acts_as_csv
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 acts_as_csv_module.rb(main):015:2> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 acts_as_csv_module.rb(main):016:1>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 acts_as_csv_module.rb(main):017:1* module ClassMethods
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 acts_as_csv_module.rb(main):018:2> def acts_as_csv
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 acts_as_csv_module.rb(main):019:3> include InstanceMethods
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 acts_as_csv_module.rb(main):020:3> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 acts_as_csv_module.rb(main):021:2> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 acts_as_csv_module.rb(main):022:1>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 acts_as_csv_module.rb(main):023:1* module InstanceMethods
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 acts_as_csv_module.rb(main):024:2>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 acts_as_csv_module.rb(main):025:2* def read
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 acts_as_csv_module.rb(main):026:3> self.csv_contents = []
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 acts_as_csv_module.rb(main):027:3> filename = self.class.to_s.downcase + '.txt'
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 acts_as_csv_module.rb(main):028:3> file = File.new(filename)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 acts_as_csv_module.rb(main):029:3> @headers = file.gets.chomp.split(', ')
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 acts_as_csv_module.rb(main):030:3>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 acts_as_csv_module.rb(main):031:3* file.each do |row|
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 acts_as_csv_module.rb(main):032:4* csv_row = CsvRow.new(headers, row.chomp.split(', '))
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 acts_as_csv_module.rb(main):033:4> @csv_contents << csv_row
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 acts_as_csv_module.rb(main):034:4> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 acts_as_csv_module.rb(main):035:3> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 acts_as_csv_module.rb(main):036:2>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 acts_as_csv_module.rb(main):037:2* attr_accessor :headers, :csv_contents
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 acts_as_csv_module.rb(main):038:2> def initialize
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 acts_as_csv_module.rb(main):039:3> read
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 acts_as_csv_module.rb(main):040:3> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 acts_as_csv_module.rb(main):041:2>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 acts_as_csv_module.rb(main):042:2* def each()
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 acts_as_csv_module.rb(main):043:3> self.csv_contents.each { |row| yield row }
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 acts_as_csv_module.rb(main):044:3> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 acts_as_csv_module.rb(main):045:2> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 acts_as_csv_module.rb(main):046:1> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 => :each
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 acts_as_csv_module.rb(main):047:0>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 acts_as_csv_module.rb(main):048:0* class RubyCsv # no inheritance! You can mix it in
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 acts_as_csv_module.rb(main):049:1> include ActsAsCsv
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 acts_as_csv_module.rb(main):050:1> # Also, nothing to do beyond the "include" - it automatically defines the methods!
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 acts_as_csv_module.rb(main):051:1* end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 => RubyCsv
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 acts_as_csv_module.rb(main):052:0>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 acts_as_csv_module.rb(main):053:0* class CsvRow
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 acts_as_csv_module.rb(main):054:1> attr_reader :values
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 acts_as_csv_module.rb(main):055:1>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 acts_as_csv_module.rb(main):056:1* def initialize(headers, values)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 acts_as_csv_module.rb(main):057:2> @values = Hash[headers.zip(values)]
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 acts_as_csv_module.rb(main):058:2> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 acts_as_csv_module.rb(main):059:1>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 acts_as_csv_module.rb(main):060:1* def method_missing(methId, *args)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 acts_as_csv_module.rb(main):061:2> str = methId.id2name
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 acts_as_csv_module.rb(main):062:2> if @values.key?(str)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 acts_as_csv_module.rb(main):063:3> return @values[str]
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 acts_as_csv_module.rb(main):064:3> else
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 acts_as_csv_module.rb(main):065:3* super.method_missing(methId)
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 acts_as_csv_module.rb(main):066:3> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 acts_as_csv_module.rb(main):067:2> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 acts_as_csv_module.rb(main):068:1> end
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 => :method_missing
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 acts_as_csv_module.rb(main):069:0>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 acts_as_csv_module.rb(main):070:0* m = RubyCsv.new
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 => #<RubyCsv:0x0000000212caf8 @csv_contents=[#<CsvRow:0x0000000212c558 @values={"first"=>"Yukihiro", "last"=>"Matsumoto", "country"=>"Japan"}>, #<CsvRow:0x0000000212c170 @values={"first"=>"Bruce", "last"=>"Tate", "country"=>"USA"}>], @headers=["first", "last", "country"]>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 acts_as_csv_module.rb(main):071:0> puts m.headers.inspect
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 ["first", "last", "country"]
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 => nil
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 acts_as_csv_module.rb(main):072:0> puts m.csv_contents.inspect
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 [#<CsvRow:0x0000000212c558 @values={"first"=>"Yukihiro", "last"=>"Matsumoto", "country"=>"Japan"}>, #<CsvRow:0x0000000212c170 @values={"first"=>"Bruce", "last"=>"Tate", "country"=>"USA"}>]
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 => nil
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 acts_as_csv_module.rb(main):073:0> # Use method_missing to implement heading-as-a-method
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 acts_as_csv_module.rb(main):074:0* puts m.csv_contents[0].country
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 Japan
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 => nil
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 acts_as_csv_module.rb(main):075:0> # And show that we still get normal failure behaviour
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 acts_as_csv_module.rb(main):076:0* puts m.csv_contents[0].Country
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 NoMethodError: undefined method `Country' for #<CsvRow:0x0000000212c558>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 from acts_as_csv_module.rb:65:in `method_missing'
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 from acts_as_csv_module.rb:76
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 from /usr/bin/irb:11:in `<main>'
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 acts_as_csv_module.rb(main):077:0>
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 acts_as_csv_module.rb(main):078:0*
2ea3b5e9b41b Implement day 3 self-study extensions:
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 acts_as_csv_module.rb(main):078:0>