加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby中的“$”是什么意思?

发布时间:2020-12-16 22:41:55 所属栏目:百科 来源:网络整理
导读:我偶然发现了rails源代码中的这段代码: # File actionpack/lib/action_view/helpers/output_safety_helper.rb,line 30def safe_join(array,sep=$,) sep ||= "".html_safe sep = ERB::Util.html_escape(sep) array.map { |i| ERB::Util.html_escape(i) }.joi
我偶然发现了rails源代码中的这段代码:
# File actionpack/lib/action_view/helpers/output_safety_helper.rb,line 30
def safe_join(array,sep=$,)
  sep ||= "".html_safe
  sep = ERB::Util.html_escape(sep)

  array.map { |i| ERB::Util.html_escape(i) }.join(sep).html_safe
end

什么是$,做什么?我读了Regexp-documentation但我找不到任何关于它的东西.

解决方法

我终于找到了自己的答案 here:

The output field separator for the print. Also,it is the default separator for Array#join. (Mnemonic: what is printed when there is a,in your print statement.)

以下代码段显示了效果:

a = [1,2,3]

puts a.join # => 123

$,= ','
puts a.join # => 1,3

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读