-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonnet_to_yaml.rb
executable file
·63 lines (48 loc) · 934 Bytes
/
sonnet_to_yaml.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
#!/usr/bin/env ruby
# while !the_end do
# sonnet_number = read line
# sonnet = read 14 lines
# save to yaml
# end
require 'pry'
require 'yaml'
class Sonnet
attr_accessor :number, :lines
def initialize(number, lines)
@number = number
@lines = lines
end
end
@sonnets = []
lines = []
sonnet_number = nil
counter = 0
file = File.new("sonnets.txt", "r")
file.each do |line|
if line.chomp.strip.empty?
counter == 0
next
end
if counter == 0
sonnet_number = line.chomp.strip.to_i
else
lines << line.chomp.strip
end
counter += 1
case sonnet_number
when 99
line_count = 16
when 126
line_count = 13
else
line_count = 15
end
if counter == line_count
sonnet = Sonnet.new(sonnet_number, lines)
@sonnets << sonnet
lines = []
counter = 0
end
end
file.close
yaml_file = File.open("sonnets.yml", "w") { |file| file.write(@sonnets.to_yaml)}