我最近开始使用 rspec 进行测试,所以我可能会犯错误,如果有更好的方法请纠正我
我创建了两个相关模型
let(:user) {FactoryGirl.create :user}
let!(:participation) {FactoryGirl.create :participation, user: user}
Run Code Online (Sandbox Code Playgroud)
在其中一项测试更改相关对象之一之前
context "when" do
before {participation.prize = 100}
it "" do
binding.pry
end
end
Run Code Online (Sandbox Code Playgroud)
但里面
participation.prize => 100
user.participatons.select(:prize) => nil
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?以及如何解决它?
我正在尝试做一些应该很简单的事情:我想在 *ngFor 中执行一个函数。该函数返回一个对象。我想在一种“let”语句中设置该对象,这样我就可以在 HTML 中使用它的属性:
<div *ngFor="let productGroup of getproductGroupBySomeVariable(variable)">
<!-- This is where I need to set a variable with the output of
the getProductBySomeProperty function-->
<div *ngLet="'{{getProductBySomeProperty(productGroup.someproperty)}}'
as myVar" class="ui-g">
<!-- Here I want to use and display the properties of the
object created by the function above-->
<span>{{myVar.property1}} </span>
<span>{{myVar.property2}} </span> etc....
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我想了解以下 TypeScript 行为:
下面的代码
let a: number
if (a === undefined) {
console.log("how?")
}
Run Code Online (Sandbox Code Playgroud)
抛出错误:“变量'a'在分配之前被使用。”。
但是下面的代码
let a: number
const f = (): void => {
if (a === undefined) {
console.log("how?")
}
}
f()
Run Code Online (Sandbox Code Playgroud)
工作正常并记录“如何?”。
这是为什么?而且,a === undefined如果它的类型是 ,那又如何呢number?
在下面的代码中,我不确定我是否理解为什么_nested2.
这是否意味着只有顶级定义才能概括其推断类型以匹配显式多态签名?
let toplevel1 : 'a. 'a -> int = (fun _ -> 0)
let toplevel2 : 'a. 'a -> int = (fun (_ : 'a) -> 0)
let nest1 =
let _nested1 : 'a. 'a -> int = (fun _ -> 0) in 0
let nest2 =
let _nested2 : 'a. 'a -> int = (fun (_ : 'a) -> 0) in 0
(* ^^^^^^^^^^^^^^^^^^^
Error: This definition has type 'a -> int which is less general …Run Code Online (Sandbox Code Playgroud) 回顾这个问题,我希望在数组中创建一个运行总计,但在数组的每一行中重新开始
| 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 |
导致
| 1 | 3 | 6 | 10 |
| 5 | 11 | 18 | 26 |
一种解决方案是使用 Makearray,效果很好:
=MAKEARRAY(
2,
4,
LAMBDA(r, c,
SUM(
INDEX(sheet1!A1:D2, r, 1) : INDEX(sheet1!A1:D2, r, c)
)
)
)
Run Code Online (Sandbox Code Playgroud)
不过,我现在希望使用 Let 语句更普遍地编写此内容:
=LET(
range, Sheet1!A1:D2,
MAKEARRAY(
rows(range),
Columns(range),
LAMBDA(r, c,
SUM(INDEX(range, r, 1) : INDEX(range, r, c))
)
)
)
Run Code Online (Sandbox Code Playgroud)
但这会导致
这个非常相似的公式可以正常工作(它不应该解决原始问题,而只是测试是否可以将范围传递到 Let 语句内的 lambda):
=LET(
range, Sheet1!A1:D2,
SCAN(0, range, LAMBDA(a, c, a + c + INDEX(range, 1, 1)))
)
Run Code Online (Sandbox Code Playgroud)
相同的代码还可以将范围作为参数传递到 …
(let ((x 2) (y 3)
(let ((x 7)
(z (+ x y)))
(* z x)))
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,为什么答案35,而不是70?在第二个let,x是7所以z应该是7 + 3 = 10,然后结果应该是7*10 = 70.我知道另一个是让*我在这之间非常混乱2.样本是谷歌的抓取.我已经google但是无法得到它.
我的问题是下一个,我尝试用一些变量来评估列表,使用let来赋值给这个变量
如果我这样做(def a (list * 'x 'y)),(let [x 3 y 3] (eval a))我有一个CompilerException java.lang.RuntimeException:无法在此上下文中解析符号:x,编译:(NO_SOURCE_PATH:6)
但如果我跑了
(def x 4) (def y 4),(eval a)我有一个16,无论如何,如果我再次跑(let [x 3 y 3] (eval a)),我有16,
存在一种正确绑定x和y并评估列表的方法?
TY!
我喜欢let在Scala中构造类似于Haskell的构造.我尝试了几种方法,但似乎没有一种方法.这是一些代码:
object CustomLet extends App {
val data = for (i <- 1 to 1024; j <- 1 to 512) yield (i % j) * i * (i + 1) - 1
def heavyCalc() = { println("heavyCalc called"); data.sum }
def doSomethingWithRes(res: Int) = {
println(s"${res * res}")
1
}
def cond(value: Int): Boolean = value > 256
// not really usable, even though it's an expression (2x heavyCalc calls)
def withoutLet() = if (cond(heavyCalc())) doSomethingWithRes(heavyCalc()) else 0 …Run Code Online (Sandbox Code Playgroud) 我想在let中定义两个变量,其中一个变量取决于另一个变量的值,如下所示:
(let ((a (func))
(b (if (eq a 1) 2 3)))
...)
Run Code Online (Sandbox Code Playgroud)
显然,这不是正确的方法,emacs说这a是无效的.这样做的正确方法是什么?
什么是有原因let的do块.
-- codeblock A
main = do
let a = 0
let f a = a + 1
let b = f 0
print (a,b)
-- codeblock B
main = do
a = 0
f a = a + 1
b = f 0
print (a,b)
Run Code Online (Sandbox Code Playgroud)
假设所有let没有in必须遵循=(这是真的吗?)
编译器应该能够意味着let从=和预处理/解糖codeblock B来codeblock A
let在这种情况下使用似乎是不必要的,就像你可以写,codeblock C但选择写codeblock D
-- codeblock C
main = do …Run Code Online (Sandbox Code Playgroud)