如何删除 Rails 初始值设定项中常量的尾随符号?

发布于 2024-08-31 15:54:29 字数 335 浏览 3 评论 0原文

如果它位于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扛刀软妹 2024-09-07 15:54:29

String#chop 返回以下内容的副本删除最后一个字符的字符串。它有一个对应的 String#chop! ,它也会改变字符串。

但是,您的特定错误(nil:NilClass 的未定义方法“删除”)意味着 ENV['KEY'] 返回 nil,这当然不会响应 <代码>删除消息。您可以尝试

KEY = ENV['KEY'].to_s.delete "\r"

将其强制为字符串。 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 counterpart String#chop! which mutates the string as well.

However, your particular error (undefined method 'delete' for nil:NilClass) means that ENV['KEY'] returned nil, which of course does not respond to the delete message. You could try

KEY = ENV['KEY'].to_s.delete "\r"

to coerce it to a string. nil.to_s returns the empty string, and "".delete x will still be "". On the other hand if ENV['KEY'] does correctly return a string, nothing different will happen than if you didn't include to_s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文