ruby - 为什么 '\n' 不起作用,$/是什么意思?

为什么这段代码不起作用:

"hello \nworld".each_line(separator = '\n') {|s| p s}

当这有效时?

"hello \nworld".each_line(separator = $/) {|s| p s}

最佳答案

谷歌 10 秒得到了这个:

$/ is the input record separator, newline by default.

第一个不起作用,因为您使用了单引号。反斜杠转义序列在单引号字符串中被忽略。改用双引号:

"hello \nworld".each_line(separator = "\n") {|s| p s}

关于ruby - 为什么 '\n' 不起作用,$/是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6323274/

相关文章:

ruby-on-rails-3 - Rails 3 在不注销的情况下设计更新密码

ruby-on-rails - ruby rails : How can I add a css f

perl - 为 LWP::UserAgent 设置参数时出现问题

django - 如果为空,请提供默认个人资料图片

objective-c - [pool release] 和 [pool drain] 有什么区别?

f# - 将字典键添加到 F# 中的构造函数

amazon-ec2 - 使用pscp自动接受rsa指纹

sql - 是什么导致了这种奇怪的 SQL 行为?

function - 为什么 Matlab 看不到我的函数?

java - 如何根据元素的一个字段的值对java中的链表进行排序?