annotate 1-Ruby/day2.rb @ 46:eac30c1b92da

Add more notes and try to understand the operators
author IBBoard <dev@ibboard.co.uk>
date Wed, 20 Sep 2017 20:48:37 +0100
parents 25c15f27b33d
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 #! /usr/bin/env ruby
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 ## File handling with a block
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 name = 'day2.txt'
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 def print_file(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 File.open(name, 'r') do |file|
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 puts name
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 while line = file.gets
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 puts " #{line}"
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 File.open(name, 'w') do |file|
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 file.puts "File contents go here"
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 print_file(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 File.open(name, 'r+') do |file|
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 # If we don't seek then we override
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 file.seek(0, IO::SEEK_END)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 file.puts 'Extra content here'
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 print_file(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 File.delete(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 puts "File still exists!" if File.exists?(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 # Old-skool Java style without passing a code block
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 my_file = File.open(name, 'w')
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 my_file.puts 'Non-block file content'
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 my_file.close() #Note: Close the file manually!
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 print_file(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 File.delete(name)
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 ## Converting a hash into an array
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 my_hash = { 'a' => 'one', 'b' => 'two', 'c' => 'three' }
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 puts my_hash.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 # "{}.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
40 puts my_hash.to_a.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 # 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
42 # There's also ":keys" and ":values"
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 puts my_hash.keys.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 puts my_hash.values.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 # 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
46 puts my_hash.collect { |val| val[1] }.inspect
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 ## Converting an array to a hash
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 # 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
50 # e.g. [[key1, value1], [key2, value2]]
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 # 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
52 my_array = ['one', 'two', 'three']
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 temp = []
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 # 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
55 # 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
56 # for :to_h.
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 my_hash = {}
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 my_array.each_with_index do |val, idx|
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 my_hash[idx] = val
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 end
ddcd809319ac Add first two Day 2 exercises
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 puts my_hash.inspect
20
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
62
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
63 ## Iterating a hash
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
64 puts "Each with single parameter gets pairs"
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
65 my_hash.each { |vals| puts "++#{vals[0]} = #{vals[1]}" }
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
66 puts "Each with two parameters gets key and value"
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
67 my_hash.each { |key, val| puts "++#{key} = #{val}" }
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
68 puts "Or you can get just keys and just values"
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
69 my_hash.each_key { |key| puts "++#{key}" }
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
70 my_hash.each_value { |value| puts "++#{value}" }
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
71
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
72 ## Arrays as stacks:
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
73 my_arr = []
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
74 my_arr.push(1)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
75 my_arr.push(2)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
76 my_arr.push(3)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
77 my_arr.push(4)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
78 puts my_arr.pop()
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
79 puts my_arr.pop()
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
80 puts my_arr.pop()
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
81 puts my_arr.pop()
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
82 ## Arrays as queues
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
83 my_arr.push(7)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
84 my_arr.push(8)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
85 my_arr.push(9)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
86 my_arr.push(10)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
87 puts my_arr.shift()
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
88 puts my_arr.shift()
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
89 puts my_arr.shift()
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
90 puts my_arr.shift()
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
91
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
92 ## Iterating in fours
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
93
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
94 my_arr = Array(1..16)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
95 # We need a temp place holder - we can't do ugly things with indexes
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
96 temp = []
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
97 my_arr.each do |val|
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
98 temp.push(val)
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
99 if temp.size == 4
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
100 puts "#{temp[0]} #{temp[1]} #{temp[2]} #{temp[3]}"
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
101 temp = []
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
102 end
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
103 end
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
104
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
105 ## And with each_slice
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
106 my_arr.each_slice(4) do |val|
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
107 puts "#{val[0]} #{val[1]} #{val[2]} #{val[3]}"
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
108 end
25c15f27b33d Complete iteration tasks for day 2
IBBoard <dev@ibboard.co.uk>
parents: 19
diff changeset
109