一元问号(?)运算符做什么?

时间:2022-02-25 22:25:26

I saw this operator in HAML code. I wonder what it is for.

我在HAML代码中看到这个操作符。我想知道这是干什么用的。

I see the following works:

我看到以下作品:

> ?{
=> "{" 
> ?\s
=> " " 
> ?a
=> "a" 

And this doesn't work:

这不起作用:

> ?ab
SyntaxError: (irb):4: syntax error, unexpected '?'

So I suppose that it takes a character a argument and returns a string with that character.

所以我假设它取一个字符一个参数然后返回一个带那个字符的字符串。

questions:

问题:

  1. What does this operator do?
  2. 这个操作员做什么?
  3. When should one use it?
  4. 什么时候应该使用它?
  5. If it really only creates a one-character string, why was it included in the language? Doesn't it break the language orthogonality? What is the benefit?
  6. 如果它真的只创建一个字符字符串,为什么要将它包含在语言中呢?它是否打破了语言的正交性?好处是什么?

3 个解决方案

#1


24  

It returns a single character string. It is the shortest way to write a single-character string literal. Use it when you want to define a lot of single-character strings. It is a heritage from Ruby <1.9, where it used to return the ASCII code for that character. I don't understand what you mean by "break the language orthogonality".

它返回一个字符串。这是写单字符字符串文字的最短方法。当您想定义许多单字符字符串时,请使用它。它是Ruby <1.9的一个传统版本,用于返回该字符的ASCII代码。我不明白你所说的“打破语言的正交性”是什么意思。

#2


9  

It's not an operator, it's a character literal. However, there is no character type in Ruby, so instead of an instance of a character type the character literal evaluates to the "default representation of a character". In Ruby 1.9+, that's a String of length 1, in Ruby 1.8, it's a Fixnum denoting the Unicode codepoint of the character.

它不是一个运算符,而是一个文字字符。但是,在Ruby中没有字符类型,因此字符文本不是字符类型的实例,而是计算为“字符的默认表示”。在Ruby 1.9+中,这是一个长度为1的字符串,在Ruby 1.8中,这是一个表示字符Unicode码点的固定数字。

#3


2  

Re #2, a place I've found it useful is in conveying that a parameter I'm setting or value I'm testing for is intended to be a single character and not just that this happened to simply be a short string. It's a subtle readability/documentation thing, but worth considering for later maintainers (including myself).

对于第二个问题,我发现它很有用的一点是,我正在设置或测试的参数应该是单个字符,而不仅仅是一个短字符串。这是一个很微妙的可读性/文档化的东西,但是值得考虑以后的维护人员(包括我自己)。

#1


24  

It returns a single character string. It is the shortest way to write a single-character string literal. Use it when you want to define a lot of single-character strings. It is a heritage from Ruby <1.9, where it used to return the ASCII code for that character. I don't understand what you mean by "break the language orthogonality".

它返回一个字符串。这是写单字符字符串文字的最短方法。当您想定义许多单字符字符串时,请使用它。它是Ruby <1.9的一个传统版本,用于返回该字符的ASCII代码。我不明白你所说的“打破语言的正交性”是什么意思。

#2


9  

It's not an operator, it's a character literal. However, there is no character type in Ruby, so instead of an instance of a character type the character literal evaluates to the "default representation of a character". In Ruby 1.9+, that's a String of length 1, in Ruby 1.8, it's a Fixnum denoting the Unicode codepoint of the character.

它不是一个运算符,而是一个文字字符。但是,在Ruby中没有字符类型,因此字符文本不是字符类型的实例,而是计算为“字符的默认表示”。在Ruby 1.9+中,这是一个长度为1的字符串,在Ruby 1.8中,这是一个表示字符Unicode码点的固定数字。

#3


2  

Re #2, a place I've found it useful is in conveying that a parameter I'm setting or value I'm testing for is intended to be a single character and not just that this happened to simply be a short string. It's a subtle readability/documentation thing, but worth considering for later maintainers (including myself).

对于第二个问题,我发现它很有用的一点是,我正在设置或测试的参数应该是单个字符,而不仅仅是一个短字符串。这是一个很微妙的可读性/文档化的东西,但是值得考虑以后的维护人员(包括我自己)。