使用这个Hash对象
{"foo" => {"bar" => 1, "baz" => 2}, "bla" => [1,2,3]}
Run Code Online (Sandbox Code Playgroud)
我想生成这个Hash对象数组
[
{"foo" => "*", "bla" => [1,2,3]},
{"foo" => {"bar" => "*", "baz" => 2}, "bla" => [1,2,3]},
{"foo" => {"bar" => "1", "baz" => "*"}, "bla" => [1,2,3]},
{"foo" => {"bar" => "*", "baz" => 2}, "bla" => "*"},
]
Run Code Online (Sandbox Code Playgroud)
我基本上遍历了每个键并将其值更改为"*",同时保留了散列的整体结构并保存了在某个数组中生成的新散列.
我已经尝试了很多想法,但大多数都不会工作,因为我之前可以猜到Array类型,我只知道这个哈希是由JSON.parse生成的,然后变成Hash(String,JSON :: Any)
我目前正在尝试它
hash = {"bar" => {"and" => "2", "br" => "1"}}
arr = [hash, {"bar" => "1"}]
arr.delete(arr.last)
arr.delete(hash)
def changer(hash, arr, …
Run Code Online (Sandbox Code Playgroud) 我写了一个程序,一个水晶程序,用 Sieve 计算一个范围内的素数。
#!/usr/bin/env crystal
def sieve(max)
t = Thread.new do
dot, ary, colours = ".", ["\xE2\xA0\x81", "\xE2\xA0\x88", "\xE2\xA0\xA0", "\xE2\xA0\x84"] * 2, [154, 184, 208, 203, 198, 164, 129, 92]
print "\e[?25l"
loop do
ary.size.times do |x|
print("\e[2K#{ary[x]} \e[38;5;#{colours[x]}mPlease Wait#{dot * x}\e[0m\r")
sleep(0.1)
end
end
end
s = [nil, nil] + (2..max).to_a
s.each do |x|
next unless x
break if (sq = x ** 2) > max
(sq..max).step(x) { |y| s[y] = nil }
end
puts "\e[?25h"
s.tap { …
Run Code Online (Sandbox Code Playgroud) 我有以下课程:
class X
property son, val
def initialize(@val : Int32)
@son = nil.as X?
end
def add(other : X?)
unless other.nil?
if @son.nil?
@son = other
else
@son.add(other)
end
end
end
end
x = X.new 5
x.add(nil)
x.add(X.new 3)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试build
我得到
Showing last frame. Use --error-trace for full trace.
In nil-test.cr:12:22
12 | @son.add(other)
^------
Error: undefined method 'include' for Nil (compile-time type is (X | Nil))
Run Code Online (Sandbox Code Playgroud)
根据手册,这正是编译器应该识别出@son
不能nil
在else
分支中的情况,但它显然没有这样做。
我究竟做错了什么 ?
注意:使用 …
为什么 Dart 会使用 Crystal Lang 的 base64 产生错误“位置 61 处的无效字符”?
我有这个Python代码:
# some_dic is a dictionary
value = some_dic.get(var_name, None)
Run Code Online (Sandbox Code Playgroud)
我怎样才能在水晶中做同样的事情?