English 中文(简体)
问题在法名末尾的含义是什么?
原标题:What does the question mark at the end of a method name mean in Ruby?
  • 时间:2009-08-28 09:05:51
  •  标签:

问题标识操作者在鲁比拉有什么目的?

有时似乎就是如此:

assert !product.valid?

有时,它载于<条码>。

最佳回答

这是一种代号公约;它表明,一种方法回报了一种毛豆价值(真实或虚假)或一种表示真实价值的物体(或“真实”价值)。

问题标识在方法名称末尾是有效的。

https://docs.ruby-lang.org/en/2.0.0/syntax/methods_rdoc.html#label-Method+Names

问题回答

注<代码>?,加上自Ruby 1.9以来单方指示的短身性质。

例如:

?F # => is the same as "F"

https://docs.ruby-lang.org/en/3.0.0/doc/syntax/literals_rdoc.html#label-Strings”rel=“nofollow noreferer”>。

There is also a character literal notation to represent single character strings, which syntax is a question mark (?) followed by a single character or escape sequence that corresponds to a single codepoint in the script encoding:

?a       #=> "a"
?abc     #=> SyntaxError
?
      #=> "
"
?s      #=> " "
?\      #=> "\"
?u{41}  #=> "A"
?C-a    #=> "x01"
?M-a    #=> "xE1"
?M-C-a #=> "x81"
?C-M-a #=> "x81", same as above
?あ      #=> "あ"

在Ruby 1.9之前,这归还了ASCII特性代码。 为了在现代鲁比拉取得旧行为,你可以使用<条码>#ord。 方法:

?F.ord # => will return 70

它是在Ruby签订的一项公约,即归还羊毛价值的方法在问题标志中结束。 它对它来说没有比这更重要的意义。

例如,它只是方法名称的一部分。 在鲁比,你还可以在方法名称中使用点名!

鲁比拉的另一个问题标志是私人经营者。

customerName == "Fred" ? "Hello Fred" : "Who are you?"

阁下

product.valid?

是否实际称为职能,并称之为“valid?。 某些类型的“有条件测试”/英国功能在公约规定的功能名称中有一个问题标志。

我认为,这只是一个包罗万象的东西的公约。 简称“IsValid

该款还经常使用,意思是“在前述特性的多数重复”

例如,定期表述<代码>/hey?/与“he和“hey相吻合”。

它还是一项共同公约,其第一条论点是:test http://ruby-doc.org/core-2.1.1/Kernel.html#method-i-test”rel=“nofollow noreferer”>。

test ?d, "/dev" # directory exists?
# => true
test ?-, "/etc/hosts", "/etc/hosts" # are the files identical
# => true

载于https://stackoverflow.com/questions/5535980/teste-argv0>





相关问题
热门标签