从初始化中调用自定义 getter 方法

发布于 2024-11-03 04:39:05 字数 1046 浏览 0 评论 0原文

我正在尝试创建一个可以执行以下操作的类: 1) 为属性实现自定义 getter 2) 从初始化方法中调用自定义 getter

如下所示:

class Book

  # RSolr lib for interacting with Solr
  require 'rsolr' 

  # Instance variables 
  @isbn
  @title

  # Solr playlist instance URL
  @solr_domain
  @solr

  def initialize(isbn)
    @solr_domain = "http://solr.com:9003/solr"
    @solr = RSolr.connect :url => @solr_domain
    @isbn = isbn
    @title = self.title(isbn)
  end

  # Get Solr URL
  def solr_domain
    return @solr_domain
  end

# Set Solr URL and reset Solr connection object
  def solr_domain(newurl)
    @solr_domain = newurl
    @solr = RSolr.connect :url => @solr_domain
  end

  # Custom getter for title
  def title=(isbn)
    result = solr.get 'select', :params => {:q => 'isbn:(' + isbn + ')'}
    return result["response"]["docs"][0]["title"]
      end
end

关键行是

@title = self.title(isbn)

我们尝试调用 title 的 getter 的地方,以便在初始化对象时设置 title。

我们想要的是一个可公开访问的 title getter,以及一种在对象初始化期间填充 @title 的方法。

I am trying to create a class that does a couple of things:
1) Implements a custom getter for an attribute
2) Calls the custom getter from within the initialize method

Here's what it looks like:

class Book

  # RSolr lib for interacting with Solr
  require 'rsolr' 

  # Instance variables 
  @isbn
  @title

  # Solr playlist instance URL
  @solr_domain
  @solr

  def initialize(isbn)
    @solr_domain = "http://solr.com:9003/solr"
    @solr = RSolr.connect :url => @solr_domain
    @isbn = isbn
    @title = self.title(isbn)
  end

  # Get Solr URL
  def solr_domain
    return @solr_domain
  end

# Set Solr URL and reset Solr connection object
  def solr_domain(newurl)
    @solr_domain = newurl
    @solr = RSolr.connect :url => @solr_domain
  end

  # Custom getter for title
  def title=(isbn)
    result = solr.get 'select', :params => {:q => 'isbn:(' + isbn + ')'}
    return result["response"]["docs"][0]["title"]
      end
end

The key lines are

@title = self.title(isbn)

where we attempt to call the getter for title, so that title gets set when the object is initialized.

What we want is a publicly accessible getter for title, as well as a way to populate @title during initialization of the object.

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

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

发布评论

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

评论(2

蓝天 2024-11-10 04:39:05

为了能够预设标题,您可以执行以下操作:

def initialize(isbn, user_title = nil)
  ...
  @title = user_title || self.title(isbn)
end

用户可以提供一个可选参数。如果他/她这样做了,@title 将被设置为该值,否则我们将自己使用 ISBN 查找标题。我希望我正确地理解了你...

编辑:顺便说一句,你确定你想要类级实例变量吗?

http://railstips.org/博客/archives/2006/11/18/class-and-instance-variables-in-ruby/

For being able to preset the title, you could do something like this:

def initialize(isbn, user_title = nil)
  ...
  @title = user_title || self.title(isbn)
end

There's an optional argument the user can supply. If he/she does, @title will get set to that value, otherwise we'll look up the title ourselves with the ISBN. I hope I understood you correctly...

Edit: BTW, are you sure you want class level instance variables?

http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/

帅哥哥的热头脑 2024-11-10 04:39:05

我不太确定你想要做什么,但我觉得你混淆了类的实例变量和实例的实例变量。如果直接在类体中写@...,就是前者,如果在实例方法定义中写@...,就是后者。看来你只想要后者。以下内容可能就是您想要的。在这里,我将 solr_domaintitle 设置为 getter,因此在某些外部类 A 中,对于 Book 的某些实例b,您可以执行b.solr_domainb.title来获取它们。在 Book 中,您不需要通过 getter 访问变量。通过名称直接访问它们@... 不会那么混乱,因此可能会减少错误。

class Book
  require 'rsolr' 
  attr_reader :solr_domain, :title
  def initialize(isbn)
    set_solr_domain("http://solr.com:9003/solr")
    @isbn = isbn
    @title = set_title
  end
  def set_solr_domain(newurl)
    @solr_domain = newurl
    @solr = RSolr.connect(:url => @solr_domain)
  end
  def set_title
    @solr.get('select', :params => {:q => "isbn:(#@isbn)"})["response"]["docs"][0]["title"]
  end
end

I am not exactly sure what you are trying to do, but I feel that you are confusing instance variables of a class and instance variables of an instance. If you directly write @... in the class body, that will be the former, if you write @... within an instance method definition, it will be the latter. It seems you just want the latter. The following is probably what you want. Here, I set solr_domain and title as getters, so within some external class A, for some instance of Book b, you can do b.solr_domain or b.title to get them. Within Book, you do not need to access variables through getters. Directly accessing them by their names @... is less confusing and hence will likely decrease bugs.

class Book
  require 'rsolr' 
  attr_reader :solr_domain, :title
  def initialize(isbn)
    set_solr_domain("http://solr.com:9003/solr")
    @isbn = isbn
    @title = set_title
  end
  def set_solr_domain(newurl)
    @solr_domain = newurl
    @solr = RSolr.connect(:url => @solr_domain)
  end
  def set_title
    @solr.get('select', :params => {:q => "isbn:(#@isbn)"})["response"]["docs"][0]["title"]
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文