I'm pretty new to Ruby and Rails but even after searching stack overflow and google I couldn't find an answer to this.
我是Ruby和Rails的新手,但即使在搜索堆栈溢出和谷歌之后我也找不到答案。
I've got a simple Ruby shorthand if statement that should return an integer
like so:
我有一个简单的Ruby简写if语句应该返回一个整数,如下所示:
# in the context of this erb document `amount` is defined as 5.
@c = ( defined? amount ? amount : r( 1,4 ) )
r()
is a custom helper function that returns a random number between in this case 1 and 4.
r()是一个自定义辅助函数,在这种情况下返回1和4之间的随机数。
The way I intend this to work is that if
amount
is defined, then use the number defined as amount
, else
generate a random number between 1 and 4 and use that instead.
我打算这样做的方法是,如果定义了金额,那么使用定义为金额的数字,否则生成1到4之间的随机数,然后使用它。
When printing out @c
however Ruby outputs expression
rather than a number.
当打印出@c时,Ruby输出表达式而不是数字。
What do I have to do to get this working as I intended and what am I doing wrong?
我需要做些什么才能让我按照我的意图工作,我做错了什么?
Many thanks for reading!
非常感谢您的阅读!
3 个解决方案
#1
9
defined?
is binding to amount ? amount : r(1,4)
so it is equivalent to:
界定?是绑定金额?金额:r(1,4)所以相当于:
defined?(amount ? amount : r(1,4))
You probably want:
你可能想要:
defined?(amount) ? amount : r(1,4)
Actually, odds are that amount || r(1,4)
, or amount.nil? ? r(1,4) : amount
would better match what you want, since I think you don't want this:
实际上,赔率是|| r(1,4),或amount.nil? ? r(1,4):金额会更符合你想要的,因为我觉得你不想要这个:
1.9.3p194 :001 > defined?(amount) => nil 1.9.3p194 :002 > amount = nil => nil 1.9.3p194 :003 > defined?(amount) => "local-variable"
...in which case @c
would be nil
- the value of the defined variable.
...在这种情况下,@ c将为nil - 已定义变量的值。
#2
3
Use the ||
operator in this case:
使用||在这种情况下运算符:
@c = amount || r (1,4)
In your code, the defined?
method operates on amount ? amount : r( 1,4 )
instead of just on amount
as you intended. Also, the defined?
operator probably doesn't do what you expect, have a look at this blog entry to get an idea.
在你的代码中,定义了什么?方法对金额有效吗?金额:r(1,4)而不仅仅是您想要的金额。还有,定义了吗?运营商可能没有按照您的期望做,看看这篇博客文章就可以了解。
#3
2
You're looking for the null coalescing operator. Try this:
您正在寻找空合并运算符。尝试这个:
@c = amount || r(1,4)
This code will assign amount to @c if amount is defined. If not it will assign the result of r(1,4) to @c.
如果定义了金额,此代码将为@c分配金额。如果不是,它会将r(1,4)的结果赋给@c。
http://eddiema.ca/2010/07/07/the-null-coalescing-operator-c-ruby-js-python/
#1
9
defined?
is binding to amount ? amount : r(1,4)
so it is equivalent to:
界定?是绑定金额?金额:r(1,4)所以相当于:
defined?(amount ? amount : r(1,4))
You probably want:
你可能想要:
defined?(amount) ? amount : r(1,4)
Actually, odds are that amount || r(1,4)
, or amount.nil? ? r(1,4) : amount
would better match what you want, since I think you don't want this:
实际上,赔率是|| r(1,4),或amount.nil? ? r(1,4):金额会更符合你想要的,因为我觉得你不想要这个:
1.9.3p194 :001 > defined?(amount) => nil 1.9.3p194 :002 > amount = nil => nil 1.9.3p194 :003 > defined?(amount) => "local-variable"
...in which case @c
would be nil
- the value of the defined variable.
...在这种情况下,@ c将为nil - 已定义变量的值。
#2
3
Use the ||
operator in this case:
使用||在这种情况下运算符:
@c = amount || r (1,4)
In your code, the defined?
method operates on amount ? amount : r( 1,4 )
instead of just on amount
as you intended. Also, the defined?
operator probably doesn't do what you expect, have a look at this blog entry to get an idea.
在你的代码中,定义了什么?方法对金额有效吗?金额:r(1,4)而不仅仅是您想要的金额。还有,定义了吗?运营商可能没有按照您的期望做,看看这篇博客文章就可以了解。
#3
2
You're looking for the null coalescing operator. Try this:
您正在寻找空合并运算符。尝试这个:
@c = amount || r(1,4)
This code will assign amount to @c if amount is defined. If not it will assign the result of r(1,4) to @c.
如果定义了金额,此代码将为@c分配金额。如果不是,它会将r(1,4)的结果赋给@c。
http://eddiema.ca/2010/07/07/the-null-coalescing-operator-c-ruby-js-python/