-
Notifications
You must be signed in to change notification settings - Fork 0
/
41.rb
88 lines (67 loc) · 2.76 KB
/
41.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'open-uri'
WORD_URL = "http://learncodethehardway.org/words.txt" #word list
WORDS = []
PHRASES = {
"class ### < ###\nend" =>
"Make a class named ### that is-a ###.",
"class ###\n\tdef initialize(@@@)\n\tend\nend" =>
"class ### has-a initialize that takes @@@ parameters.",
"class ###\n\tdef ***(@@@)\n\tend\nend" =>
"class ### has-a function named *** that takes @@@ parameters.",
"*** = ###.new()" =>
"Set *** to an instance of class ###.",
"***.***(@@@)" =>
"From *** get the *** function, and call it with parameters @@@.",
"***.*** = '***'" =>
"From *** get the *** attribute and set it to '***'."
}
PHRASE_FIRST = ARGV[0] == "english" #true se primo arg == english
open(WORD_URL) {|f| #parse wrods list into array
f.each_line {|word| WORDS.push(word.chomp)}
}
def craft_names(rand_words, snippet, pattern, caps=false) #create an array with the exact number of words
names = snippet.scan(pattern).map do #scan for snippet, use the array size to count words to take
word = rand_words.pop() #act on param so no duplicates, uhm.
caps ? word.capitalize : word
end
return names * 2 #why?
end
def craft_params(rand_words, snippet, pattern)
names = (0...snippet.scan(pattern).length).map do #stessa cosa
param_count = rand(3) + 1 #the name that will be pushed to get changed is made out of 1-3 parameters
params = (0...param_count).map {|x| rand_words.pop() }
params.join(', ')
end
return names * 2
end
def convert(snippet, phrase)
rand_words = WORDS.sort_by {rand} #new order every call
class_names = craft_names(rand_words, snippet, /###/, caps=true)
other_names = craft_names(rand_words, snippet, /\*\*\*/)
param_names = craft_params(rand_words, snippet, /@@@/)
results = []
[snippet, phrase].each do |sentence| #actual substution taking place here
# fake class names, also copies sentence
result = sentence.gsub(/###/) {|x| class_names.pop }
# fake other names
result.gsub!(/\*\*\*/) {|x| other_names.pop }
# fake parameter lists
result.gsub!(/@@@/) {|x| param_names.pop }
results.push(result)
end
return results
end
# keep going until they hit CTRL-D
loop do
snippets = PHRASES.keys().sort_by {rand} #randomize order
for snippet in snippets #loop all before a new randomize
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase) #take the current answer/question pair and plug it with random words
if PHRASE_FIRST
question, answer = answer, question
end
print question, "\n\n> "
exit(0) unless $stdin.gets
puts "\nANSWER: %s\n\n" % answer
end
end