cattr_accessor 位于 Rails 之外

发布于 2024-09-05 08:58:42 字数 287 浏览 7 评论 0原文

我正在尝试使用 google_search ruby​​ 库(代码如下),但它抱怨 'cattr_accessor 是一个未定义的方法' - 你知道为什么会这样或者我如何修复它吗?

require 'rubygems'
require 'google_search'

GoogleSearch.web :q => "pink floyd"

I'm trying to use the google_search ruby library (code follows) but it complains that 'cattr_accessor is an undefined method' - any ideas why this might be or how I could fix it?

require 'rubygems'
require 'google_search'

GoogleSearch.web :q => "pink floyd"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

無處可尋 2024-09-12 08:58:42

cattr_accessor 似乎是一个 Rails 扩展 ,其行为类似于 attr_accessor ,但可以在类及其实例上访问

如果您想复制 cattr_accessor 方法的源代码,请查看 本文档

# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 46
def cattr_accessor(*syms)
  cattr_reader(*syms)
  cattr_writer(*syms)
end

# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 4
def cattr_reader(*syms)
  syms.flatten.each do |sym|
    next if sym.is_a?(Hash)
    class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}\n@@\#{sym}\nend\n\ndef \#{sym}\n@@\#{sym}\nend\n", __FILE__, __LINE__)
  end
end

# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 24
def cattr_writer(*syms)
  options = syms.extract_options!
  syms.flatten.each do |sym|
    class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}=(obj)\n@@\#{sym} = obj\nend\n\n\#{\"\ndef \#{sym}=(obj)\n@@\#{sym} = obj\nend\n\" unless options[:instance_writer] == false }\n", __FILE__, __LINE__)
  end
end

cattr_accessor seems to be a Rails extension that acts like attr_accessor, but is accessible on both the class and its instances.

If you want to copy the source of the cattr_accessor method, check out this documentation:

# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 46
def cattr_accessor(*syms)
  cattr_reader(*syms)
  cattr_writer(*syms)
end

# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 4
def cattr_reader(*syms)
  syms.flatten.each do |sym|
    next if sym.is_a?(Hash)
    class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}\n@@\#{sym}\nend\n\ndef \#{sym}\n@@\#{sym}\nend\n", __FILE__, __LINE__)
  end
end

# File vendor/rails/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb, line 24
def cattr_writer(*syms)
  options = syms.extract_options!
  syms.flatten.each do |sym|
    class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}=(obj)\n@@\#{sym} = obj\nend\n\n\#{\"\ndef \#{sym}=(obj)\n@@\#{sym} = obj\nend\n\" unless options[:instance_writer] == false }\n", __FILE__, __LINE__)
  end
end
月下凄凉 2024-09-12 08:58:42

您可以通过包含 Ruby Facets gem 来获得此功能。在此处引用源:

https://github.com/ rubyworks/facets/blob/master/lib/core/facets/cattr.rb

您通常不需要要求 gem 中的所有代码。您可以选择性地要求您想要的东西。不过 gem 中有很多有用的扩展。

You can get this functionality by including the Ruby Facets gem. Reference the source here:

https://github.com/rubyworks/facets/blob/master/lib/core/facets/cattr.rb

You generally don't need to require all code from the gem. You can selectively require what you want. There are quite a few useful extensions in the gem though.

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