Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,304 +13,426 @@ app.logger = logging.getLogger("CodeSearchAPI")
|
|
13 |
|
14 |
# 预定义代码片段
|
15 |
CODE_SNIPPETS = [
|
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 |
-
"rename('
|
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 |
-
"class
|
107 |
-
"
|
108 |
-
"class
|
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 |
"str[0]",
|
173 |
-
"str[
|
174 |
-
"
|
175 |
-
"
|
176 |
-
"
|
177 |
-
"
|
178 |
-
"
|
179 |
-
"
|
180 |
-
"
|
181 |
-
"
|
182 |
-
"
|
183 |
-
"
|
184 |
-
"
|
185 |
-
"
|
186 |
-
"
|
187 |
-
"
|
188 |
-
"require
|
189 |
-
"
|
190 |
-
"
|
191 |
-
"require
|
192 |
-
|
193 |
-
"
|
194 |
-
"""
|
195 |
-
|
196 |
-
"""
|
197 |
-
|
198 |
-
"""
|
199 |
-
"""
|
200 |
-
|
201 |
-
|
202 |
-
"""
|
203 |
-
|
204 |
-
|
205 |
-
"""
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
"
|
211 |
-
"
|
212 |
-
"
|
213 |
-
"""
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
"
|
218 |
-
"
|
219 |
-
|
220 |
-
"
|
221 |
-
|
222 |
-
"
|
223 |
-
"
|
224 |
-
"
|
225 |
-
"
|
226 |
-
"
|
227 |
-
"
|
228 |
-
"
|
229 |
-
"
|
230 |
-
"
|
231 |
-
"
|
232 |
-
|
233 |
-
"
|
234 |
-
"
|
235 |
-
"
|
236 |
-
|
237 |
-
"
|
238 |
-
"
|
239 |
-
"
|
240 |
-
"
|
241 |
-
"
|
242 |
-
"
|
243 |
-
"
|
244 |
-
"
|
245 |
-
|
246 |
-
|
247 |
-
"
|
248 |
-
"
|
249 |
-
"
|
250 |
-
"
|
251 |
-
|
252 |
-
"
|
253 |
-
"
|
254 |
-
"
|
255 |
-
"
|
256 |
-
"
|
257 |
-
"
|
258 |
-
"
|
259 |
-
"
|
260 |
-
"
|
261 |
-
"
|
262 |
-
"
|
263 |
-
"
|
264 |
-
"
|
265 |
-
"
|
266 |
-
"
|
267 |
-
"
|
268 |
-
"
|
269 |
-
"
|
270 |
-
"
|
271 |
-
"
|
272 |
-
"
|
273 |
-
"
|
274 |
-
"
|
275 |
-
"
|
276 |
-
"
|
277 |
-
"
|
278 |
-
"
|
279 |
-
"
|
280 |
-
"
|
281 |
-
|
282 |
-
|
283 |
-
"
|
284 |
-
"
|
285 |
-
|
286 |
-
"
|
287 |
-
"
|
288 |
-
"
|
289 |
-
"
|
290 |
-
"
|
291 |
-
"
|
292 |
-
|
293 |
-
|
294 |
-
"
|
295 |
-
|
296 |
-
"
|
297 |
-
"
|
298 |
-
|
299 |
-
"
|
300 |
-
"
|
301 |
-
"
|
302 |
-
"
|
303 |
-
"
|
304 |
-
"
|
305 |
-
"
|
306 |
-
"
|
307 |
-
"
|
308 |
-
|
309 |
-
"
|
310 |
-
"
|
311 |
-
"
|
312 |
-
"
|
313 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
314 |
|
315 |
|
316 |
|
|
|
13 |
|
14 |
# 预定义代码片段
|
15 |
CODE_SNIPPETS = [
|
16 |
+
|
17 |
+
|
18 |
+
"puts 'Hello, World!'",
|
19 |
+
"def sum(a, b); a + b; end",
|
20 |
+
"rand",
|
21 |
+
"def even?(num); num.even?; end",
|
22 |
+
"str.length",
|
23 |
+
"Date.today",
|
24 |
+
"File.exist?('file.txt')",
|
25 |
+
"File.read('file.txt')",
|
26 |
+
"File.write('file.txt', 'content')",
|
27 |
+
"Time.now",
|
28 |
+
"str.upcase",
|
29 |
+
"str.downcase",
|
30 |
+
"str.reverse",
|
31 |
+
"list.size",
|
32 |
+
"list.max",
|
33 |
+
"list.min",
|
34 |
+
"list.sort",
|
35 |
+
"list1 + list2",
|
36 |
+
"list.delete(element)",
|
37 |
+
"list.empty?",
|
38 |
+
"str.count(char)",
|
39 |
+
"str.include?(substring)",
|
40 |
+
"num.to_s",
|
41 |
+
"str.to_i",
|
42 |
+
"str.match?(/^\d+$/)",
|
43 |
+
"list.index(element)",
|
44 |
+
"list.clear",
|
45 |
+
"list.reverse",
|
46 |
+
"list.uniq",
|
47 |
+
"list.include?(value)",
|
48 |
+
"{}",
|
49 |
+
"hash[key] = value",
|
50 |
+
"hash.delete(key)",
|
51 |
+
"hash.keys",
|
52 |
+
"hash.values",
|
53 |
+
"hash1.merge(hash2)",
|
54 |
+
"hash.empty?",
|
55 |
+
"hash[key]",
|
56 |
+
"hash.key?(key)",
|
57 |
+
"hash.clear",
|
58 |
+
"File.readlines('file.txt').size",
|
59 |
+
"File.write('file.txt', list.join('\\n'))",
|
60 |
+
"File.read('file.txt').split('\\n')",
|
61 |
+
"File.read('file.txt').split.size",
|
62 |
+
"def leap_year?(year); (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0); end",
|
63 |
+
"Time.now.strftime('%Y-%m-%d %H:%M:%S')",
|
64 |
+
"(Date.today - Date.new(2023, 1, 1)).to_i",
|
65 |
+
"Dir.pwd",
|
66 |
+
"Dir.entries('.')",
|
67 |
+
"Dir.mkdir('new_dir')",
|
68 |
+
"Dir.rmdir('new_dir')",
|
69 |
+
"File.file?('path')",
|
70 |
+
"File.directory?('path')",
|
71 |
+
"File.size('file.txt')",
|
72 |
+
"File.rename('old.txt', 'new.txt')",
|
73 |
+
"FileUtils.cp('source.txt', 'destination.txt')",
|
74 |
+
"FileUtils.mv('source.txt', 'destination.txt')",
|
75 |
+
"File.delete('file.txt')",
|
76 |
+
"ENV['VAR_NAME']",
|
77 |
+
"ENV['VAR_NAME'] = 'value'",
|
78 |
+
"system('open https://example.com')",
|
79 |
+
"require 'net/http'; Net::HTTP.get(URI('https://example.com'))",
|
80 |
+
"require 'json'; JSON.parse(json_string)",
|
81 |
+
"require 'json'; File.write('file.json', JSON.dump(data))",
|
82 |
+
"require 'json'; JSON.parse(File.read('file.json'))",
|
83 |
+
"list.join",
|
84 |
+
"str.split(',')",
|
85 |
+
"list.join(',')",
|
86 |
+
"list.join('\\n')",
|
87 |
+
"str.split",
|
88 |
+
"str.split(delimiter)",
|
89 |
+
"str.chars",
|
90 |
+
"str.gsub(old, new)",
|
91 |
+
"str.gsub(' ', '')",
|
92 |
+
"str.gsub(/[^a-zA-Z0-9]/, '')",
|
93 |
+
"str.empty?",
|
94 |
+
"str == str.reverse",
|
95 |
+
"require 'csv'; CSV.open('file.csv', 'w') { |csv| csv << ['data'] }",
|
96 |
+
"require 'csv'; CSV.read('file.csv')",
|
97 |
+
"require 'csv'; CSV.read('file.csv').size",
|
98 |
+
"list.shuffle",
|
99 |
+
"list.sample",
|
100 |
+
"list.sample(n)",
|
101 |
+
"rand(6) + 1",
|
102 |
+
"rand(2) == 0 ? 'Heads' : 'Tails'",
|
103 |
+
"SecureRandom.alphanumeric(8)",
|
104 |
+
"format('#%06x', rand(0xffffff))",
|
105 |
+
"SecureRandom.uuid",
|
106 |
+
"class MyClass; end",
|
107 |
+
"MyClass.new",
|
108 |
+
"class MyClass; def my_method; end; end",
|
109 |
+
"class MyClass; attr_accessor :my_attr; end",
|
110 |
+
"class ChildClass < ParentClass; end",
|
111 |
+
"class ChildClass < ParentClass; def my_method; super; end; end",
|
112 |
+
"class MyClass; def self.class_method; end; end",
|
113 |
+
"class MyClass; def self.static_method; end; end",
|
114 |
+
"obj.is_a?(Class)",
|
115 |
+
"obj.instance_variable_get(:@attr)",
|
116 |
+
"obj.instance_variable_set(:@attr, value)",
|
117 |
+
"obj.instance_variable_defined?(:@attr)",
|
118 |
+
"begin; risky_operation; rescue => e; puts e; end",
|
119 |
+
"""class CustomError < StandardError
|
120 |
+
end
|
121 |
+
raise CustomError, 'error occurred'""",
|
122 |
+
"begin; raise 'oops'; rescue => e; e.message; end",
|
123 |
+
"""require 'logger'
|
124 |
+
logger = Logger.new('error.log')
|
125 |
+
logger.error('error occurred')""",
|
126 |
+
"start_time = Time.now",
|
127 |
+
"Time.now - start_time",
|
128 |
+
"20.times { |i| print "\r[#{'='(i+1)}#{' '(19-i)}]"; sleep(0.1) }",
|
129 |
+
"sleep(1)",
|
130 |
+
"square = ->(x) { xx }",
|
131 |
+
"squares = [1,2,3].map { |n| nn }",
|
132 |
+
"evens = [1,2,3,4].select { |n| n.even? }",
|
133 |
+
"sum = [1,2,3].reduce(0) { |acc,n| acc+n }",
|
134 |
+
"doubles = [1,2,3,4,5].map { |n| n2 }",
|
135 |
+
"hash = [1,2,3].map { |n| [n, n2] }.to_h",
|
136 |
+
"require 'set'; s = Set.new([1,2,3].map { |n| n*2 })",
|
137 |
+
"intersection = a & b",
|
138 |
+
"union = a | b",
|
139 |
+
"diff = a - b",
|
140 |
+
"filtered = list.compact",
|
141 |
+
"begin; File.open('file.txt'); rescue; false; end",
|
142 |
+
"x.is_a?(String)",
|
143 |
+
"bool = ['true','1'].include?(str.downcase)",
|
144 |
+
"puts 'yes' if x > 0",
|
145 |
+
"i=0; while i<5; i+=1; end",
|
146 |
+
"for item in [1,2,3]; puts item; end",
|
147 |
+
"""h = {a:1, b:2}
|
148 |
+
for k, v in h
|
149 |
+
puts "#{k}:#{v}"
|
150 |
+
end""",
|
151 |
+
"""for c in 'hello'.chars
|
152 |
+
puts c
|
153 |
+
end""",
|
154 |
+
"""for i in 1..5
|
155 |
+
break if i==3
|
156 |
+
puts i
|
157 |
+
end""",
|
158 |
+
"""for i in 1..5
|
159 |
+
next if i==3
|
160 |
+
puts i
|
161 |
+
end""",
|
162 |
+
"def foo; end",
|
163 |
+
"def foo(a=1); a; end",
|
164 |
+
"def foo; [1,2]; end",
|
165 |
+
"def foo(*args); args; end",
|
166 |
+
"def foo(a:, b:); a+b; end",
|
167 |
+
"""def foo
|
168 |
+
end
|
169 |
+
start = Time.now
|
170 |
+
foo
|
171 |
+
puts Time.now - start""",
|
172 |
+
"""def decorate(f)
|
173 |
+
->(args) { puts 'before'; result = f.call(args); puts 'after'; result }
|
174 |
+
end""",
|
175 |
+
"""def fib(n, memo={})
|
176 |
+
return memo[n] if memo[n]
|
177 |
+
memo[n] = n<2 ? n : fib(n-1, memo) + fib(n-2, memo)
|
178 |
+
end""",
|
179 |
+
"gen = Enumerator.new { |y| i=0; loop { y << i; i+=1 } }",
|
180 |
+
"def foo; yield 1; end",
|
181 |
+
"gen.next",
|
182 |
+
"itr = [1,2,3].each",
|
183 |
+
"""itr = [1,2,3].each
|
184 |
+
loop do
|
185 |
+
puts itr.next
|
186 |
+
end""",
|
187 |
+
"[1,2].each_with_index { |v, i| puts i, v }",
|
188 |
+
"zipped = [1,2].zip(['a','b'])",
|
189 |
+
"h = [1,2].zip(['a','b']).to_h",
|
190 |
+
"[1,2] == [1,2]",
|
191 |
+
"{a:1, b:2} == {b:2, a:1}",
|
192 |
+
"require 'set'; Set.new([1,2]) == Set.new([2,1])",
|
193 |
+
"unique = [1,2,1].uniq",
|
194 |
+
"s.clear",
|
195 |
+
"s.empty?",
|
196 |
+
"s.add(1)",
|
197 |
+
"s.delete(1)",
|
198 |
+
"s.include?(1)",
|
199 |
+
"s.size",
|
200 |
+
"!(a & b).empty?",
|
201 |
+
"[1,2].all? { |e| [1,2,3].include?(e) }",
|
202 |
+
"'hi'.include?('h')",
|
203 |
"str[0]",
|
204 |
+
"str[-1]",
|
205 |
+
"File.extname(path) == '.txt'",
|
206 |
+
"['.png','.jpg','.jpeg','.gif'].include?(File.extname(path))",
|
207 |
+
"x.round",
|
208 |
+
"x.ceil",
|
209 |
+
"x.floor",
|
210 |
+
"sprintf('%.2f', x)",
|
211 |
+
"require 'securerandom'; SecureRandom.alphanumeric(8)",
|
212 |
+
"File.exist?('path')",
|
213 |
+
"Dir['**/'].each { |f| puts f }",
|
214 |
+
"File.extname('path.txt')",
|
215 |
+
"File.basename(path)",
|
216 |
+
"File.expand_path(path)",
|
217 |
+
"RUBY_VERSION",
|
218 |
+
"RUBY_PLATFORM",
|
219 |
+
"require 'etc'; Etc.nprocessors",
|
220 |
+
"mem = grep MemTotal /proc/meminfo",
|
221 |
+
"df = df -h /",
|
222 |
+
"""require 'socket'
|
223 |
+
ip = Socket.ip_address_list.detect(&:ipv4_private).ip_address""",
|
224 |
+
"system('ping -c1 8.8.8.8 > /dev/null 2>&1')",
|
225 |
+
"""require 'open-uri'
|
226 |
+
File.open('file', 'wb') { |f| f.write open(url).read }""",
|
227 |
+
"""def upload(file)
|
228 |
+
puts 'Uploading'
|
229 |
+
end""",
|
230 |
+
"""require 'net/http'
|
231 |
+
uri = URI(url)
|
232 |
+
Net::HTTP.post_form(uri, key: 'value')""",
|
233 |
+
"""uri = URI(url)
|
234 |
+
uri.query = URI.encode_www_form(params)
|
235 |
+
Net::HTTP.get(uri)""",
|
236 |
+
"""require 'net/http'
|
237 |
+
uri = URI(url)
|
238 |
+
req = Net::HTTP::Get.new(uri)
|
239 |
+
req['User-Agent'] = 'Custom'
|
240 |
+
res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }""",
|
241 |
+
"require 'nokogiri'; doc = Nokogiri::HTML(html)",
|
242 |
+
"doc.at('title').text",
|
243 |
+
"links = doc.css('a').map { |a| a['href'] }",
|
244 |
+
"""doc.css('img').each do |img|
|
245 |
+
open(img['src']).each do |chunk|
|
246 |
+
File.open(File.basename(img['src']), 'ab') { |f| f.write chunk }
|
247 |
+
end
|
248 |
+
end""",
|
249 |
+
"""freq = Hash.new(0)
|
250 |
+
text.split.each { |w| freq[w] += 1 }""",
|
251 |
+
"""require 'net/http'
|
252 |
+
res = Net::HTTP.post_form(URI(login_url), username: 'u', password: 'p')""",
|
253 |
+
"Nokogiri::HTML(html).text",
|
254 |
+
"emails = text.scan(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/)",
|
255 |
+
"phones = text.scan(/\b\d{3}-\d{3}-\d{4}\b/)",
|
256 |
+
"nums = text.scan(/\d+/)",
|
257 |
+
"new = text.gsub(/foo/, 'bar')",
|
258 |
+
"!!(text =~ /pattern/)",
|
259 |
+
"clean = text.gsub(/<[^>]>/, '')",
|
260 |
+
"CGI.escapeHTML(text)",
|
261 |
+
"CGI.unescapeHTML(text)",
|
262 |
+
"""require 'tk'
|
263 |
+
root = TkRoot.new { title 'App' }
|
264 |
+
Tk.mainloop""",
|
265 |
+
"require 'tk'",
|
266 |
+
"""root = TkRoot.new
|
267 |
+
button = TkButton.new(root) {text 'Click Me'; command { Tk.messageBox(message: 'Button Clicked!') }}
|
268 |
+
button.pack""",
|
269 |
+
"""Tk.messageBox(message: 'Hello, World!')""",
|
270 |
+
"""entry = TkEntry.new(root).pack
|
271 |
+
entry.get""",
|
272 |
+
"""root.title = 'My Window'""",
|
273 |
+
"""root.geometry('400x300')""",
|
274 |
+
"""root.geometry('+%d+%d' % [(root.winfo_screenwidth() - root.winfo_reqwidth()) / 2, (root.winfo_screenheight() - root.winfo_reqheight()) / 2])""",
|
275 |
+
"""menu = TkMenu.new(root)
|
276 |
+
root['menu'] = menu
|
277 |
+
menu.add('command', 'label' => 'File')""",
|
278 |
+
"""combobox = Tk::Tile::Combobox.new(root).pack""",
|
279 |
+
"""radio = TkRadioButton.new(root) {text 'Option 1'}.pack""",
|
280 |
+
"""check = TkCheckButton.new(root) {text 'Check Me'}.pack""",
|
281 |
+
"""image = TkPhotoImage.new(file: 'image.png')
|
282 |
+
label = TkLabel.new(root) {image image}.pack""",
|
283 |
+
"""`afplay audio.mp3`""",
|
284 |
+
"""`ffplay video.mp4`""",
|
285 |
+
"""`ffmpeg -i video.mp4 -f null - 2>&1 | grep 'time=' | awk '{print $2}'`""",
|
286 |
+
"""`screencapture screen.png`""",
|
287 |
+
"""`ffmpeg -f avfoundation -i "1" -t 10 screen.mp4`""",
|
288 |
+
"""`cliclick p:.`""",
|
289 |
+
"""`cliclick kd:cmd kp:space ku:cmd`""",
|
290 |
+
"""`cliclick c:.`""",
|
291 |
+
"""Time.now.to_i""",
|
292 |
+
"""Time.at(timestamp).strftime('%Y-%m-%d')""",
|
293 |
+
"""Time.parse(date).to_i""",
|
294 |
+
"""Time.now.strftime('%A')""",
|
295 |
+
"""Time.days_in_month(Time.now.month, Time.now.year)""",
|
296 |
+
"""Time.new(Time.now.year, 1, 1)""",
|
297 |
+
"""Time.new(Time.now.year, 12, 31)""",
|
298 |
+
"""Time.new(year, month, 1)""",
|
299 |
+
"""Time.new(year, month, -1)""",
|
300 |
+
"""Time.now.wday.between?(1, 5)""",
|
301 |
+
"""Time.now.wday.between?(6, 7)""",
|
302 |
+
"""Time.now.hour""",
|
303 |
+
"""Time.now.min""",
|
304 |
+
"""Time.now.sec""",
|
305 |
+
"""sleep(1)""",
|
306 |
+
"""(Time.now.to_f * 1000).to_i""",
|
307 |
+
"""Time.now.strftime('%Y-%m-%d %H:%M:%S')""",
|
308 |
+
"""Time.parse(time_str)""",
|
309 |
+
"""Thread.new { puts 'Hello from thread' }""",
|
310 |
+
"""sleep(1)""",
|
311 |
+
"""threads = []
|
312 |
+
3.times { threads << Thread.new { puts 'Hello from thread' } }
|
313 |
+
threads.each(&:join)""",
|
314 |
+
"""Thread.current.name""",
|
315 |
+
"""thread = Thread.new { puts 'Hello from thread' }
|
316 |
+
thread.abort_on_exception = true""",
|
317 |
+
"""mutex = Mutex.new
|
318 |
+
mutex.synchronize { puts 'Hello from synchronized thread' }""",
|
319 |
+
"""pid = Process.spawn('sleep 5')""",
|
320 |
+
"""Process.pid""",
|
321 |
+
"""Process.kill(0, pid) rescue false""",
|
322 |
+
"""pids = []
|
323 |
+
3.times { pids << Process.spawn('sleep 5') }
|
324 |
+
pids.each { |pid| Process.wait(pid) }""",
|
325 |
+
"""queue = Queue.new
|
326 |
+
queue.push('Hello')
|
327 |
+
queue.pop""",
|
328 |
+
"""reader, writer = IO.pipe
|
329 |
+
writer.puts 'Hello'
|
330 |
+
reader.gets""",
|
331 |
+
"""Process.setrlimit(:CPU, 50)""",
|
332 |
+
"""`ls`""",
|
333 |
+
"""`ls`.chomp""",
|
334 |
+
"""$?.exitstatus""",
|
335 |
+
"""$?.success?""",
|
336 |
+
"""File.expand_path(__FILE__)""",
|
337 |
+
"""ARGV""",
|
338 |
+
"""require 'optparse'
|
339 |
+
OptionParser.new { |opts| opts.on('-h', '--help', 'Show help') { puts opts } }.parse!""",
|
340 |
+
"""OptionParser.new { |opts| opts.on('-h', '--help', 'Show help') { puts opts } }.parse!""",
|
341 |
+
"""Gem.loaded_specs.keys""",
|
342 |
+
"""`gem install package_name`""",
|
343 |
+
"""`gem uninstall package_name`""",
|
344 |
+
"""Gem.loaded_specs['package_name'].version.to_s""",
|
345 |
+
"""`bundle exec ruby script.rb`""",
|
346 |
+
"""Gem::Specification.map(&:name)""",
|
347 |
+
"""`gem update package_name`""",
|
348 |
+
"""require 'sqlite3'
|
349 |
+
db = SQLite3::Database.new('test.db')""",
|
350 |
+
"""db.execute('SELECT * FROM table')""",
|
351 |
+
"""db.execute('INSERT INTO table (column) VALUES (?)', 'value')""",
|
352 |
+
"""db.execute('DELETE FROM table WHERE id = ?', 1)""",
|
353 |
+
"""db.execute('UPDATE table SET column = ? WHERE id = ?', 'new_value', 1)""",
|
354 |
+
"""db.execute('SELECT * FROM table').each { |row| puts row }""",
|
355 |
+
"""db.execute('SELECT * FROM table WHERE column = ?', 'value')""",
|
356 |
+
"""db.close""",
|
357 |
+
"""db.execute('CREATE TABLE table (id INTEGER PRIMARY KEY, column TEXT)')""",
|
358 |
+
"""db.execute('DROP TABLE table')""",
|
359 |
+
"""db.table_info('table').any?""",
|
360 |
+
"""db.execute('SELECT name FROM sqlite_master WHERE type = "table"')""",
|
361 |
+
"""class Model < ActiveRecord::Base
|
362 |
+
end
|
363 |
+
Model.create(column: 'value')""",
|
364 |
+
"""Model.find_by(column: 'value')""",
|
365 |
+
"""Model.find_by(column: 'value').destroy""",
|
366 |
+
"""Model.find_by(column: 'value').update(column: 'new_value')""",
|
367 |
+
"""class Model < ActiveRecord::Base
|
368 |
+
end""",
|
369 |
+
"""class ChildModel < ParentModel
|
370 |
+
end""",
|
371 |
+
"""class Model < ActiveRecord::Base
|
372 |
+
self.primary_key = 'id'
|
373 |
+
end""",
|
374 |
+
"""class Model < ActiveRecord::Base
|
375 |
+
validates_uniqueness_of :column
|
376 |
+
end""",
|
377 |
+
"""class Model < ActiveRecord::Base
|
378 |
+
attribute :column, default: 'value'
|
379 |
+
end""",
|
380 |
+
"""require 'csv'
|
381 |
+
CSV.open('data.csv', 'w') { |csv| csv << ['column1', 'column2'] }""",
|
382 |
+
"""require 'spreadsheet'
|
383 |
+
book = Spreadsheet::Workbook.new
|
384 |
+
sheet = book.create_worksheet
|
385 |
+
sheet[0, 0] = 'Hello'
|
386 |
+
book.write('data.xls')""",
|
387 |
+
"""require 'json'
|
388 |
+
File.write('data.json', {key: 'value'}.to_json)""",
|
389 |
+
"""require 'spreadsheet'
|
390 |
+
book = Spreadsheet.open('data.xls')
|
391 |
+
sheet = book.worksheet(0)
|
392 |
+
sheet.each { |row| puts row }""",
|
393 |
+
"""require 'spreadsheet'
|
394 |
+
book1 = Spreadsheet.open('file1.xls')
|
395 |
+
book2 = Spreadsheet.open('file2.xls')
|
396 |
+
book1.worksheets.each { |sheet| book2.add_worksheet(sheet) }
|
397 |
+
book2.write('merged.xls')""",
|
398 |
+
"""require 'spreadsheet'
|
399 |
+
book = Spreadsheet::Workbook.new
|
400 |
+
book.create_worksheet(name: 'New Sheet')
|
401 |
+
book.write('data.xls')""",
|
402 |
+
"""require 'spreadsheet'
|
403 |
+
book = Spreadsheet.open('data.xls')
|
404 |
+
sheet = book.worksheet(0)
|
405 |
+
new_sheet = book.create_worksheet
|
406 |
+
new_sheet.format_with(sheet)
|
407 |
+
book.write('data.xls')""",
|
408 |
+
"""require 'spreadsheet'
|
409 |
+
book = Spreadsheet.open('data.xls')
|
410 |
+
sheet = book.worksheet(0)
|
411 |
+
sheet.row(0).set_format(0, Spreadsheet::Format.new(color: :red))
|
412 |
+
book.write('data.xls')""",
|
413 |
+
"""require 'spreadsheet'
|
414 |
+
book = Spreadsheet.open('data.xls')
|
415 |
+
sheet = book.worksheet(0)
|
416 |
+
sheet.row(0).set_format(0, Spreadsheet::Format.new(weight: :bold))
|
417 |
+
book.write('data.xls')""",
|
418 |
+
"""require 'spreadsheet'
|
419 |
+
book = Spreadsheet.open('data.xls')
|
420 |
+
sheet = book.worksheet(0)
|
421 |
+
sheet[0, 0]""",
|
422 |
+
"""require 'spreadsheet'
|
423 |
+
book = Spreadsheet::Workbook.new
|
424 |
+
sheet = book.create_worksheet
|
425 |
+
sheet[0, 0] = 'Hello'
|
426 |
+
book.write('data.xls')""",
|
427 |
+
"""require 'rmagick'
|
428 |
+
image = Magick::Image.read('image.png').first
|
429 |
+
[image.columns, image.rows]""",
|
430 |
+
"""require 'rmagick'
|
431 |
+
image = Magick::Image.read('image.png').first
|
432 |
+
image.resize!(100, 100)"""
|
433 |
+
|
434 |
+
|
435 |
+
|
436 |
|
437 |
|
438 |
|