我有一个方法在Cat模型上执行一些操作,如果输入错误引发异常:
context "hungry cat" do
it { expect { eat(what: nil) }.to raise_error }
end
Run Code Online (Sandbox Code Playgroud)
我想要做的是检查这种方法是否会改变猫的状态,如下所示:
context "hungry cat" do
it { expect { eat(what: nil) }.to raise_error }
it { expect { eat(what: nil) }.not_to change(cat, :status) }
end
Run Code Online (Sandbox Code Playgroud)
问题是,既然eat(what: nil)会引发异常,那么it无论如何都会失败.那么,是否可以忽略异常并检查某些条件?
我知道可以这样做:
it do
expect do
begin
eat(what: nil)
rescue
end
end.not_to change(cat, :status)
end
Run Code Online (Sandbox Code Playgroud)
但这太难看了.
当我编写算法时,我通常会在注释中写下不变量.
例如,一个函数可能返回一个有序列表,另一个函数则期望列出一个列表.
我知道定理证明存在,但我没有使用它们的经验.
我也相信智能编译器[sic!]可以利用它们来优化程序.
那么,是否有可能写下不变量并让编译器检查它们?
是否有一些功能,str.isnumeric但适用于浮动?
'13.37'.isnumeric() #False
Run Code Online (Sandbox Code Playgroud)
我还是用这个:
def isFloat(string):
try:
float(string)
return True
except ValueError:
return False
Run Code Online (Sandbox Code Playgroud) 例如我有字符串:
aacbbbqq
Run Code Online (Sandbox Code Playgroud)
结果我想要以下匹配:
(aa, c, bbb, qq)
Run Code Online (Sandbox Code Playgroud)
我知道我可以这样写:
([a]+)|([b]+)|([c]+)|...
Run Code Online (Sandbox Code Playgroud)
但我认为我很丑,并寻求更好的解决方案.我正在寻找正则表达式解决方案,而不是自编有限状态机.
有没有类似于这个C++模板的东西?
template <int A>
class B
{
int f()
{
return A;
}
}
Run Code Online (Sandbox Code Playgroud)
我想使B <1>,B <2>等的每个实例(例如元组)成为不同的类型.
我有一个非常大的数字:5799218898.并希望将其右移到13位.
所以,windows-calculator或python给了我:
5799218898 >> 13 | 100010100100001110011111100001 >> 13 70791 | 10001010010000111
正如所料.
但是Javascript:
5799218898 >> 13 | 100010100100001110011111100001 >> 13 183624 | 101100110101001000
我认为这是因为javascript中的内部整数表示,但找不到任何相关内容.
我想分成ByteString这样的话:
import qualified Data.ByteString as BS
main = do
input <- BS.getLine
let xs = BS.split ' ' input
Run Code Online (Sandbox Code Playgroud)
但似乎GHC无法将字符文字转换为Word8自身,所以我得到:
Couldn't match expected type `GHC.Word.Word8'
with actual type `Char'
In the first argument of `BS.split', namely ' '
In the expression: BS.split ' ' input
Run Code Online (Sandbox Code Playgroud)
Hoogle没有找到类型签名的任何内容,Char -> Word8并且Word.Word8 ' '是无效的类型构造函数.关于如何修复它的任何想法?
在经历了很多PHP经验之后,我正在学习Python,并且在Python中使用类型提示会很方便.看起来Eclipse与PyDev不支持这一点.有什么建议?
例如,我希望我的IDE 在我使用它时显示函数文档字符串和类型,例如:
def f(x: int) -> int:
r"""Adds 3 to x"""
return x + 3
f(# and now IDE shows everything about types
Run Code Online (Sandbox Code Playgroud) 我有两个版本的应用程序布局,它们只有几行不同.考虑以下示例:
!!!
%html
%head
# a lot of code here
%body
# some more code here
- if defined? flag and flag == true
# variant 1
- else
# variant 2
Run Code Online (Sandbox Code Playgroud)
问题是,如何将此标志传递给布局?
class ApplicationController < ActionController::Base
layout 'layout', :locals => {:flag => true} #won't work :(
# ...
end
Run Code Online (Sandbox Code Playgroud) 我在我的一个Rails 3.1应用程序上使用带有ActiveRecord的state_machine.我发现访问具有不同状态的记录的语法很麻烦.是否可以在不编写范围定义的情况下同时将每个状态定义为范围?
考虑以下示例:
class User < ActiveRecord:Base
state_machine :status, :initial => :foo do
state :foo
state :bar
# ...
end
end
# state_machine syntax:
User.with_status :foo
User.with_status :bar
# desired syntax:
User.foo
User.bar
Run Code Online (Sandbox Code Playgroud) python ×3
ruby ×3
haskell ×2
bit-shift ×1
bytestring ×1
c# ×1
c++ ×1
generics ×1
invariants ×1
javascript ×1
layout ×1
pydev ×1
regex ×1
rspec ×1
security ×1
templates ×1
testing ×1
type-hinting ×1
types ×1
unit-testing ×1