我使用Postgres的JSON数据类型来存储一些信息.
例如,我的模型User包含一个字段locations,该字段包含一个json文档(包含键和值对的对象数组),格式如下:
[{"name": "Location 1", kind: "house"},
{"name": "Location 2", kind: "house"},
{"name": "Location 3", kind: "office"},
...
{"name": "Location X", kind: "house"}
]
Run Code Online (Sandbox Code Playgroud)
我想查询.whereJSON数据类型.
我想查询至少有一个位置的用户kind = office.
谢谢!
我有需要以"并行"运行的代码(不是真的,我知道Crystal不支持并行性).
require "http/client"
thread_count = 4
resps = [] of HTTP::Client::Response
mutex = Thread::Mutex.new
urls = [] of String
(1..10).each { |i| urls << "http://httpbin.org/delay#{i}"}
threads = Array.new(thread_count) {
Thread.new do
while url = mutex.synchronize { urls.pop? }
resp = HTTP::Client.get(url)
mutex.synchronize { resps << resp }
end
end
}
threads.map(&.join)
Run Code Online (Sandbox Code Playgroud)
Thread该类的源文件表示不使用它.无论如何,这段代码不起作用HTTP::Client.
所以我有各种哈希值,它们并不总是具有相同的键/值对.我想要做的是能够合并散列,但是如果它们不存在于该散列中,则添加空键/值对,但在其他散列中也是如此.这很难解释,但这可能会更好地解释它:
t1 = Merger.new({"a" => "1"})
puts t1.merge({:b => 2})
# => {"a" => "1", :b => 2}
t2 = Merger.new({"a" => "1", :b => 2})
puts t2.merge({:c => "3"})
# => {"a" => "1", :b => 2, :c => "3"}
t3 = Merger.new({"a" => "1", "b" => 2})
puts t3.merge
# => {"a" => "1", "b" => 2}
t4 = Merger.new
puts t4.merge({:a => 1})
# => {:a => 1}
t5 = Merger.new
puts t4.merge
# => {}
Run Code Online (Sandbox Code Playgroud)
Merger …