What is the purpose of the question mark operator in Ruby?
Ruby中的问号操作符的目的是什么?
Sometimes it appears like this:
有时它看起来是这样的:
assert !product.valid?
sometimes it's in an if
construct.
有时它在一个if结构中。
9 个解决方案
#1
227
It is a code style convention; it indicates that a method returns a boolean value.
这是一种代码风格的约定;它指示方法返回布尔值。
The question mark is a valid character at the end of a method name.
问号是方法名末尾的有效字符。
#2
70
Also note ?
along with a character, will return the ASCII character code for A
还要注意吗?与字符一起,将返回a的ASCII字符代码
For example:
例如:
?F # => will return 70
Alternately in ruby 1.8 you can do:
或者在ruby 1.8中,你可以这样做:
"F"[0]
or in ruby 1.9:
或者在ruby 1.9:
"F".ord
Also notice that ?F
will return the string "F"
, so in order to make the code shorter, you can also use ?F.ord
in Ruby 1.9 to get the same result as "F".ord
.
注意?F将返回字符串“F”,因此为了使代码更短,还可以使用?F。在Ruby 1.9中获得与“F”相同的结果。
#3
35
It's a convention in Ruby that methods that return boolean values end in a question mark. There's no more significance to it than that.
在Ruby中,返回布尔值的方法以问号结尾,这是一种约定。没有比这更重要的了。
#4
22
In your example it's just part of the method name. In Ruby you can also use exclamation points in method names!
在您的示例中,它只是方法名的一部分。在Ruby中,您还可以使用方法名中的感叹号!
Another example of question marks in Ruby would be the ternary operator.
Ruby中问号的另一个例子是三元运算符。
customerName == "Fred" ? "Hello Fred" : "Who are you?"
#5
14
In your example
在你的例子
product.valid?
Is actually a function call and calls a function named valid?
. Certain types of "test for condition"/boolean functions have a question mark as part of the function name by convention.
函数调用和调用一个名为valid的函数?某些类型的“条件测试”/布尔函数有一个问号作为函数名的一部分。
#6
12
It may be worth pointing out that ?s are only allowed in method names, not variables. In the process of learning Ruby, I assumed that ? designated a boolean return type so I tried adding them to flag variables, leading to errors. This led to me erroneously believing for a while that there was some special syntax involving ?s.
值得指出的是,?s只允许在方法名中使用,而不允许在变量中使用。在学习Ruby的过程中,我认为是这样的吗?指定了一个布尔返回类型,因此我尝试将它们添加到标记变量中,从而导致错误。这使我错误地认为有一些特殊的语法涉及到?s。
Relevant: Why can't a variable name end with `?` while a method name can?
相关:为什么变量名不能以'结尾?“而一个方法名可以?”
#7
5
I believe it's just a convention for things that are boolean. A bit like saying "IsValid
".
我认为这只是布尔值的惯例。有点像说“是有效的”。
#8
1
It's also used in regular expressions, meaning "at most one repetition of the preceding character"
它也被用在正则表达式中,意思是“最多一次重复前面的字符”
for example the regular expression /hey?/
matches with the strings "he
" and "hey
".
比如正则表达式/嘿?/匹配字符串“he”和“hey”。
#9
0
It's also a common convention to use with the first argument of the test method from Kernel#test
这也是与来自内核#test的测试方法的第一个参数一起使用的常见约定
irb(main):001:0> test ?d, "/dev" # directory exists?
=> true
irb(main):002:0> test ?-, "/etc/hosts", "/etc/hosts" # are the files identical
=> true
#1
227
It is a code style convention; it indicates that a method returns a boolean value.
这是一种代码风格的约定;它指示方法返回布尔值。
The question mark is a valid character at the end of a method name.
问号是方法名末尾的有效字符。
#2
70
Also note ?
along with a character, will return the ASCII character code for A
还要注意吗?与字符一起,将返回a的ASCII字符代码
For example:
例如:
?F # => will return 70
Alternately in ruby 1.8 you can do:
或者在ruby 1.8中,你可以这样做:
"F"[0]
or in ruby 1.9:
或者在ruby 1.9:
"F".ord
Also notice that ?F
will return the string "F"
, so in order to make the code shorter, you can also use ?F.ord
in Ruby 1.9 to get the same result as "F".ord
.
注意?F将返回字符串“F”,因此为了使代码更短,还可以使用?F。在Ruby 1.9中获得与“F”相同的结果。
#3
35
It's a convention in Ruby that methods that return boolean values end in a question mark. There's no more significance to it than that.
在Ruby中,返回布尔值的方法以问号结尾,这是一种约定。没有比这更重要的了。
#4
22
In your example it's just part of the method name. In Ruby you can also use exclamation points in method names!
在您的示例中,它只是方法名的一部分。在Ruby中,您还可以使用方法名中的感叹号!
Another example of question marks in Ruby would be the ternary operator.
Ruby中问号的另一个例子是三元运算符。
customerName == "Fred" ? "Hello Fred" : "Who are you?"
#5
14
In your example
在你的例子
product.valid?
Is actually a function call and calls a function named valid?
. Certain types of "test for condition"/boolean functions have a question mark as part of the function name by convention.
函数调用和调用一个名为valid的函数?某些类型的“条件测试”/布尔函数有一个问号作为函数名的一部分。
#6
12
It may be worth pointing out that ?s are only allowed in method names, not variables. In the process of learning Ruby, I assumed that ? designated a boolean return type so I tried adding them to flag variables, leading to errors. This led to me erroneously believing for a while that there was some special syntax involving ?s.
值得指出的是,?s只允许在方法名中使用,而不允许在变量中使用。在学习Ruby的过程中,我认为是这样的吗?指定了一个布尔返回类型,因此我尝试将它们添加到标记变量中,从而导致错误。这使我错误地认为有一些特殊的语法涉及到?s。
Relevant: Why can't a variable name end with `?` while a method name can?
相关:为什么变量名不能以'结尾?“而一个方法名可以?”
#7
5
I believe it's just a convention for things that are boolean. A bit like saying "IsValid
".
我认为这只是布尔值的惯例。有点像说“是有效的”。
#8
1
It's also used in regular expressions, meaning "at most one repetition of the preceding character"
它也被用在正则表达式中,意思是“最多一次重复前面的字符”
for example the regular expression /hey?/
matches with the strings "he
" and "hey
".
比如正则表达式/嘿?/匹配字符串“he”和“hey”。
#9
0
It's also a common convention to use with the first argument of the test method from Kernel#test
这也是与来自内核#test的测试方法的第一个参数一起使用的常见约定
irb(main):001:0> test ?d, "/dev" # directory exists?
=> true
irb(main):002:0> test ?-, "/etc/hosts", "/etc/hosts" # are the files identical
=> true