即使 require 语句有效,也无法使用 gem 库
我尝试在脚本中使用 activeSupport 的空白方法并收到错误“未定义方法‘空白?’”对于“blah”:字符串(NoMethodError)”。 Ruby 的 require 语句没有问题,但我无法使用该库。
require "rubygems"
require "active_support"
if "blah".blank?
puts "blank!"
end
I'm trying to use activeSupport's blank method in a script and getting the error "undefined method 'blank?' for "blah":String (NoMethodError)". Ruby doesn't have an issue with the require statement but I can't use the library.
require "rubygems"
require "active_support"
if "blah".blank?
puts "blank!"
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的是哪个版本的 ActiveSupport?在当前版本中,默认情况下它不再将每个功能加载到命名空间中。相反,您可以准确挑选您需要的功能。对于您的情况:
如果您想要一切,请使用
实际上,在现代版本的 Ruby 中,您也可以摆脱
require 'rubygems'
。即使在旧版本中,您也不应该将其放在那里,因为它会强制代码的每个用户都使用 RubyGems,并且无法在 RubyGems 不可用的环境中使用。Which version of ActiveSupport are you using? In current versions, it does no longer load every single feature into the namespace by default. Instead, you can pick and choose exactly which features you need. In your case:
If you want everything, use
Actually, in modern versions of Ruby, you can get rid of the
require 'rubygems'
as well. And even in older versions, you shouldn't put that there, since it forces every user of your code to use RubyGems and makes it impossible to use in environments where RubyGems is not available.嗯,我刚刚尝试过,效果很好(没有打印任何内容)。
Hmm, I just tried this and it worked fine (didn't print anything).