我想获得带有补丁号的ruby版本.但RUBY_VERSION只返回前三个数字.
所以我这样写:
`ruby -v`.gsub(/ruby (\d\.\d\.\dp\d+) .*\n/, $1)
Run Code Online (Sandbox Code Playgroud)
但这有点棘手.有没有更好的方法来做到这一点?
我在RSpec中有这个代码.
before(:each) do
@company = Company.create(price: 700)
allow(@company).to receive(:foo){5}
end
Run Code Online (Sandbox Code Playgroud)
我也可以使用allow方法let吗?如果是,我该如何使用它?
我有一系列像这样的哈希:
hashes = [{date: Date.today, value: 1},
{date: Date.today - 1.day, value: 2},
{date: Date.today - 2.day, value: 3},
{date: Date.today + 1.day, value: 4}
]
Run Code Online (Sandbox Code Playgroud)
我想得到最近的约会.我可以得到:
hashes.sort{|i| i[:date]}.first[:date]
# => Sat, 27 Feb 2016
Run Code Online (Sandbox Code Playgroud)
但我觉得这有点多余.有没有更好的方法来实现这种行为?
这两个表达似乎没有区别.
h = {a: 1, b: 2}
h.merge({c: 3, d: 4})
h.merge(c: 3, d: 4)
Run Code Online (Sandbox Code Playgroud)
如果我在Hash用作参数时省略花括号,是否存在问题?
如果单独提供键和值,我该如何制作哈希?我现在就这样做.
h = Hash.new
values.each_with_index do |v, i|
h[keys[i]] = v
end
Run Code Online (Sandbox Code Playgroud)
但我认为有更好的方法可以做到这一点.有什么建议吗?
在一个食谱中,nginx我使用了一个service资源。
service 'nginx' do
supports status: true, restart: true, reload: true
action [:enable, :start]
end
Run Code Online (Sandbox Code Playgroud)
在另一个配方中foo,notifies :reload, 'service[nginx]'使用了。
当我运行这两个食谱或 only 时nginx,它工作正常。但是当只foo在 a 中时run_list,它会失败并出现错误service[nginx] cannot be found in the resource collection.
目前,当我只想运行foo.
knife solo cook my_server --override-runlist "nginx,foo`
Run Code Online (Sandbox Code Playgroud)
我添加depends 'nginx'到foo/metadate.rb,但它没有解决问题。
如何指定这样的依赖项?
我认为[[:space:]]匹配所有类似空格的字符,但"零宽度空间"是一个例外.
# normal space
32.chr('UTF-8').match?(/[[:space:]]/) #=> true
# no break space
160.chr('UTF-8').match?(/[[:space:]]/) #=> true
# en space
8194.chr('UTF-8').match?(/[[:space:]]/) #=> true
# em space
8195.chr('UTF-8').match?(/[[:space:]]/) #=> true
# thin space
8201.chr('UTF-8').match?(/[[:space:]]/) #=> true
# ideographic space
12288.chr('UTF-8').match?(/[[:space:]]/) #=> true
# zero width space
8203.chr('UTF-8').match?(/[[:space:]]/) #=> false
# zero width no break space
65279.chr('UTF-8').match?(/[[:space:]]/) #=> false
Run Code Online (Sandbox Code Playgroud)
如何编写匹配所有这些空格的正则表达式?