Mercurial > repos > other > SevenLanguagesInSevenWeeks
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 |
rev | line source |
---|---|
19 | 1 #! /usr/bin/env ruby |
2 | |
3 ## File handling with a block | |
4 name = 'day2.txt' | |
5 | |
6 def print_file(name) | |
7 File.open(name, 'r') do |file| | |
8 puts name | |
9 while line = file.gets | |
10 puts " #{line}" | |
11 end | |
12 end | |
13 end | |
14 | |
15 File.open(name, 'w') do |file| | |
16 file.puts "File contents go here" | |
17 end | |
18 print_file(name) | |
19 File.open(name, 'r+') do |file| | |
20 # If we don't seek then we override | |
21 file.seek(0, IO::SEEK_END) | |
22 file.puts 'Extra content here' | |
23 end | |
24 print_file(name) | |
25 File.delete(name) | |
26 | |
27 puts "File still exists!" if File.exists?(name) | |
28 | |
29 # Old-skool Java style without passing a code block | |
30 my_file = File.open(name, 'w') | |
31 my_file.puts 'Non-block file content' | |
32 my_file.close() #Note: Close the file manually! | |
33 print_file(name) | |
34 File.delete(name) | |
35 | |
36 ## Converting a hash into an array | |
37 my_hash = { 'a' => 'one', 'b' => 'two', 'c' => 'three' } | |
38 puts my_hash.inspect | |
39 # "{}.methods" includes "to_a" method - appears to convert to an array. | |
40 puts my_hash.to_a.inspect | |
41 # However, it includes keys *and* values (as nested arrays, according to http://apidock.com/ruby/Hash/to_a). | |
42 # There's also ":keys" and ":values" | |
43 puts my_hash.keys.inspect | |
44 puts my_hash.values.inspect | |
45 # Alternatively, :collect runs over all key-value pairs as an array, so we can do: | |
46 puts my_hash.collect { |val| val[1] }.inspect | |
47 | |
48 ## Converting an array to a hash | |
49 # Arrays have a :to_h method, but that assumes nested arrays of key-value pairs, | |
50 # e.g. [[key1, value1], [key2, value2]] | |
51 # Looping to create a nested array structure then :to_h would work: | |
52 my_array = ['one', 'two', 'three'] | |
53 temp = [] | |
54 # This would be neater with :map or :collect but then we don't get the index. | |
55 # The only sensible option is an :each_with_index, but that negates the need | |
56 # for :to_h. | |
57 my_hash = {} | |
58 my_array.each_with_index do |val, idx| | |
59 my_hash[idx] = val | |
60 end | |
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 |