?/在 Ruby 中是什么意思? [英] What does ?/ mean in Ruby?

查看:479
本文介绍了?/在 Ruby 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了这段代码:

I just stumbled across this piece of code:

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

这个?/"是什么?我从未见过这样写字符串.

What's this "?/"? I've never seen writing strings this way.

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

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

Apparently it works for single characters only:

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

是什么?是什么意思?

推荐答案

? 用于表示单个字符串文字.像 ?a,?b 但不是 ?ab.

? is used to represent single character string literals. Like ?a,?b but not ?ab.

要回答 OP 的评论:

是的,他们是.

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

这篇关于?/在 Ruby 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆