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