forked from makersacademy/learn_to_program
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learn2program_ch11.rb
73 lines (53 loc) · 1.57 KB
/
learn2program_ch11.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
=begin
#11.3: Saving and Loading for Grown-Ups
filename = 'ListerQuote.txt'
test_string = "I promise that I swear absolutely that " +
"I will never mention gazpacho soup again."
#Open file 'filename' and write, 'w', to it
File.open filename, 'w' do |f|
f.write test_string
end
#Read from file 'filename' and store in var 'read_string'
read_string = File.read filename
puts(read_string == test_string)
=end
=begin
#11.4: YAML pt.I
require 'yaml'
test_array = ["Give Quiche A Chance",
"Mutants Out!",
"Chameleonic Life-Forms, No Thanks"]
#convert 'test_array' object to yaml supported string and store in var 'test_string'
test_string = test_array.to_yaml
filename = "RimmerTShirts.txt"
File.open filename, 'w' do |f|
f.write test_string
end
read_string = File.read filename
#call 'load' method from YAML class to load the 'read_string' contents into 'read_array'
read_array = YAML::load read_string
puts(read_string == test_string)
puts(read_array == test_array)
=end
=begin
#11.6: YAML pt.II
require 'yaml'
#define method, using yaml, to write to a file.
def yaml_save(object,filename)
File.open filename, 'w' do |f|
f.write(object.to_yaml)
end
end
#define method, using yaml, to read from a file
def yaml_load(filename)
yaml_string = File.read filename
YAML::load yaml_string
end
test_array = ['Slick Shoes',
'Bully Blinders',
'Pinchers of Peril']
filename = 'DatasGadgets.txt'
yaml_save(test_array,filename)
read_array = yaml_load(filename)
puts(read_array == test_array)
=end