annotate 1-Ruby/day2-irb.output @ 19:ddcd809319ac

Add first two Day 2 exercises
author IBBoard <dev@ibboard.co.uk>
date Wed, 04 Jan 2017 21:01:13 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 day2.rb(main):001:0> #! /usr/bin/env ruby
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 day2.rb(main):002:0*
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 day2.rb(main):003:0* ## File handling with a block
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 day2.rb(main):004:0* name = 'day2.txt'
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 => "day2.txt"
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 day2.rb(main):005:0>
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 day2.rb(main):006:0* def print_file(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 day2.rb(main):007:1> File.open(name, 'r') do |file|
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 day2.rb(main):008:2* puts name
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 day2.rb(main):009:2> while line = file.gets
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 day2.rb(main):010:3> puts " #{line}"
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 day2.rb(main):011:3> end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 day2.rb(main):012:2> end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 day2.rb(main):013:1> end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 => :print_file
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 day2.rb(main):014:0>
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 day2.rb(main):015:0* File.open(name, 'w') do |file|
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 day2.rb(main):016:1* file.puts "File contents go here"
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 day2.rb(main):017:1> end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 day2.rb(main):018:0> print_file(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 day2.txt
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 File contents go here
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 day2.rb(main):019:0> File.open(name, 'r+') do |file|
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 day2.rb(main):020:1* # If we don't seek then we override
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 day2.rb(main):021:1* file.seek(0, IO::SEEK_END)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 day2.rb(main):022:1> file.puts 'Extra content here'
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 day2.rb(main):023:1> end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 day2.rb(main):024:0> print_file(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 day2.txt
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 File contents go here
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 Extra content here
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 day2.rb(main):025:0> File.delete(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 => 1
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 day2.rb(main):026:0>
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 day2.rb(main):027:0* puts "File still exists!" if File.exists?(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 day2.rb(main):028:0>
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 day2.rb(main):029:0* # Old-skool Java style without passing a code block
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 day2.rb(main):030:0* my_file = File.open(name, 'w')
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 => #<File:day2.txt>
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 day2.rb(main):031:0> my_file.puts 'Non-block file content'
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 day2.rb(main):032:0> my_file.close() #Note: Close the file manually!
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 day2.rb(main):033:0> print_file(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 day2.txt
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 Non-block file content
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 day2.rb(main):034:0> File.delete(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 => 1
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 day2.rb(main):035:0>
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 day2.rb(main):036:0* ## Converting a hash into an array
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 day2.rb(main):037:0* my_hash = { 'a' => 'one', 'b' => 'two', 'c' => 'three' }
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 => {"a"=>"one", "b"=>"two", "c"=>"three"}
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 day2.rb(main):038:0> puts my_hash.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 {"a"=>"one", "b"=>"two", "c"=>"three"}
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 day2.rb(main):039:0> # "{}.methods" includes "to_a" method - appears to convert to an array.
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 day2.rb(main):040:0* puts my_hash.to_a.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 [["a", "one"], ["b", "two"], ["c", "three"]]
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 day2.rb(main):041:0> # However, it includes keys *and* values (as nested arrays, according to http://apidock.com/ruby/Hash/to_a).
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 day2.rb(main):042:0* # There's also ":keys" and ":values"
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 day2.rb(main):043:0* puts my_hash.keys.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 ["a", "b", "c"]
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 day2.rb(main):044:0> puts my_hash.values.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 ["one", "two", "three"]
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 day2.rb(main):045:0> # Alternatively, :collect runs over all key-value pairs as an array, so we can do:
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 day2.rb(main):046:0* puts my_hash.collect { |val| val[1] }.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 ["one", "two", "three"]
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 day2.rb(main):047:0>
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 day2.rb(main):048:0* ## Converting an array to a hash
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 day2.rb(main):049:0* # Arrays have a :to_h method, but that assumes nested arrays of key-value pairs,
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 day2.rb(main):050:0* # e.g. [[key1, value1], [key2, value2]]
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82 day2.rb(main):051:0* # Looping to create a nested array structure then :to_h would work:
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 day2.rb(main):052:0* my_array = ['one', 'two', 'three']
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 => ["one", "two", "three"]
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 day2.rb(main):053:0> temp = []
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 => []
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87 day2.rb(main):054:0> # This would be neater with :map or :collect but then we don't get the index.
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 day2.rb(main):055:0* # The only sensible option is an :each_with_index, but that negates the need
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 day2.rb(main):056:0* # for :to_h.
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 day2.rb(main):057:0* my_hash = {}
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 => {}
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 day2.rb(main):058:0> my_array.each_with_index do |val, idx|
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 day2.rb(main):059:1* my_hash[idx] = val
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 day2.rb(main):060:1> end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 => ["one", "two", "three"]
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 day2.rb(main):061:0> puts my_hash.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 {0=>"one", 1=>"two", 2=>"three"}
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 => nil
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 day2.rb(main):062:0>