如何删除 Rails 初始值设定项中常量的尾随符号?
如果它位于 config/environment.rb 文件中,如何正确删除以下句子中的尾随字符。
KEY = ENV['KEY'].delete "\r"
它会产生以下错误:
undefined method `delete' for nil:NilClass (NoMethodError)
它在 IRB 中运行良好,但在环境中不起作用。rb
已解决
Aptana Studio 3 在最新更新后停止加载 .bashrc。感谢姆拉登和马克的帮助。
How does one remove trailing character correctly in the following sentence if it's in config/environment.rb
file.
KEY = ENV['KEY'].delete "\r"
It produces the following error:
undefined method `delete' for nil:NilClass (NoMethodError)
It works well in IRB, but not in environment.rb
Solved
Aptana Studio 3 stopped to load .bashrc after the latest update. Thanks to Mladen and Mark for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
String#chop
返回以下内容的副本删除最后一个字符的字符串。它有一个对应的String#chop!
,它也会改变字符串。但是,您的特定错误(nil:NilClass 的未定义方法“删除”)意味着
ENV['KEY']
返回nil
,这当然不会响应 <代码>删除消息。您可以尝试将其强制为字符串。
nil.to_s
返回空字符串,并且"".delete x
仍将是""
。另一方面,如果ENV['KEY']
确实正确返回字符串,则与不包含to_s
时不会发生任何不同的情况。String#chop
returns a copy of the string with the last character removed. And it has a counterpartString#chop!
which mutates the string as well.However, your particular error (undefined method 'delete' for nil:NilClass) means that
ENV['KEY']
returnednil
, which of course does not respond to thedelete
message. You could tryto coerce it to a string.
nil.to_s
returns the empty string, and"".delete x
will still be""
. On the other hand ifENV['KEY']
does correctly return a string, nothing different will happen than if you didn't includeto_s
.