-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter11archive.rb
More file actions
203 lines (155 loc) · 4.29 KB
/
chapter11archive.rb
File metadata and controls
203 lines (155 loc) · 4.29 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
require 'yaml'
test_array = [
['Rick','Brady'],
['Rebecca','Kaarin'],
['Abe','Richard'],
]
test_string = test_array.to_yaml
filename = 'rick_test.txt'
File.open filename, 'w' do |f|
f.write test_string
end
read_string = File.read(filename)
read_array = YAML::load read_string
puts(read_string == test_string)
puts(read_array == test_array)
puts test_string
puts read_array
puts read_array[2][1]
#####
buffy_quote_1 =
'\'Kiss rocks\'?
Why would anyone want to kiss...
Oh, wait. I get it.'
buffy_quote_2 =
"'Kiss rocks'?\n" +
"Why would anyone want to kiss...\n" +
"Oh, wait. I get it."
puts buffy_quote_1
puts buffy_quote_2
puts(buffy_quote_1 == buffy_quote_2)
puts "3...\n2...\n1...\nHappy New Year!"
puts "#{2 * 10 **4 + 1} Leagues Under the Sea, THE REVENGE!!!"
#####
require 'yaml'
# Define yaml save and load methods...
def yaml_save object, filename
File.open filename, 'w' do |f|
f.write(object.to_yaml)
end
end
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'
# Save it
yaml_save test_array, filename
# Load it
read_array = yaml_load filename
puts(read_array == test_array)
#####
# Moving and renaming pictures program
Dir.chdir '/Users/rick/Desktop/Chapter 11 Finished Pictures'
# First we find all of the pictures to be moved.
pic_names = Dir['/Users/rick/Desktop/Chapter 11 Picture Renaming/**/*.jpg']
puts 'What would you like to call this batch?'
batch_name = gets.chomp
puts
print "Downloading #{pic_names.length} files: "
# This is out counter. We'll start at 1 today,
# though normally I like to count from 0.
pic_number = 1
pic_names.each do |name|
print '.' # This is our "progress bar".
new_name = if pic_number < 10
"#{batch_name}0#{pic_number}.jpg"
else
"#{batch_name}#{pic_number}.jpg"
end
# New name contained the entire directory path
# new_name stripped out the directory path and
# is replaced via File.rename below.
File.rename name, new_name
pic_number = pic_number + 1
end
puts # This is so we aren't on progress bar line.
puts 'Done.'
#####
puts "Safer Picture Downloading"
# Moving and renaming pictures program
Dir.chdir '/Users/rick/Desktop/Chapter 11 Finished Pictures'
# First we find all of the pictures to be moved.
pic_names = Dir['/Users/rick/Desktop/Chapter 11 Picture Renaming/**/*.jpg']
puts 'What would you like to call this batch?'
batch_name = gets.chomp
puts
print "Downloading #{pic_names.length} files: "
# This is out counter. We'll start at 1 today,
# though normally I like to count from 0.
pic_number = 1
def renamer older_name, newer_name, picture_increment
File.rename older_name, newer_name
picture_increment = picture_increment + 1
return picture_increment
end
pic_names.each do |name|
print '.' # This is our "progress bar".
new_name = if pic_number < 10
"#{batch_name}0#{pic_number}.jpg"
else
"#{batch_name}#{pic_number}.jpg"
end
# Check to see if the new file already exists
if (File.exists? new_name) == true
puts ''
puts "#{new_name} already exists."
puts 'Would you like to skip (S), replace (R) or exit (X)?'
response = gets.chomp.capitalize
good_response = false
while good_response != true
if response == 'S'
puts "Skipping #{new_name}"
pic_number = pic_number + 1
good_response = true
elsif response == 'R'
puts "Replacing #{new_name}"
pic_number = renamer(name, new_name, pic_number)
good_response = true
elsif response == 'X'
puts 'Exiting program'
exit
else
puts 'Please enter S, R or X'
response = gets.chomp.capitalize
end
end
else
pic_number = renamer(name, new_name, pic_number)
end
end
puts # This is so we aren't on progress bar line.
puts 'Done.'
#####
puts 'Build Your Own Playlist Program v1.0'
# Each line in a text file is the path to a song.
# Give the filename an .m3u extension
# Use the shuffle method to create a playlist
# Store the playlists here
# /Users/rick/Desktop/generated_playlists
filename = 'playlist1.m3u'
Dir.chdir('/Users/rick/Desktop/generated_playlists')
# Find all of the mp3s
# /Users/rick/Desktop/music_keep
all_music = Dir['/Users/rick/Desktop/music_keep/**/*.mp3']
shufflled_music = all_music.shuffle
File.open filename, 'w' do |d|
shufflled_music.each do |x|
d.write x + "\n"
end
end