在Ruby中是什么意思?

我偶然发现了这段代码:

if source[0] != ?/ source = compute_asset_path(source, options) end 

这是什么“ ?/ ”? 我从未见过以这种方式写字符串。

 $ irb 2.0.0p247 :001 > ?/ => "/" 

显然它只适用于单个字符:

 2.0.0p247 :001 > ?a => "a" 2.0.0p247 :002 > ?foo SyntaxError: (irb):2: syntax error, unexpected '?' 

什么 意思?

? 用于表示单个字符串文字。 喜欢?a?b但不是?ab

回答OP的评论 :

对,他们是。

 irb(main):001:0> ?x + 'y' => "xy" irb(main):002:0> 'x' + 'y' => "xy" 

在Ruby 1.8.x系列中,它返回ASCII值

 alok@alok-desktop:~$ rvm use ruby-1.8.7-p370 Using /home/alok/.rvm/gems/ruby-1.8.7-p370 alok@alok-desktop:~$ irb 1.8.7-p370 :001 > ?F => 70 

在Ruby 1.9+中,它返回相同的字符串

 1.9.2-p320 :018 > ?A => "A" 
 $> "/" == ?/ => true 

另一个版本的字符串,但更短:) also true: %{/}