从初始化中调用自定义 getter 方法
我正在尝试创建一个可以执行以下操作的类: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了能够预设标题,您可以执行以下操作:
用户可以提供一个可选参数。如果他/她这样做了,
@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:
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/
我不太确定你想要做什么,但我觉得你混淆了类的实例变量和实例的实例变量。如果直接在类体中写
@...
,就是前者,如果在实例方法定义中写@...
,就是后者。看来你只想要后者。以下内容可能就是您想要的。在这里,我将solr_domain
和title
设置为 getter,因此在某些外部类A
中,对于Book
的某些实例b
,您可以执行b.solr_domain
或b.title
来获取它们。在Book
中,您不需要通过 getter 访问变量。通过名称直接访问它们@...
不会那么混乱,因此可能会减少错误。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 setsolr_domain
andtitle
as getters, so within some external classA
, for some instance ofBook
b
, you can dob.solr_domain
orb.title
to get them. WithinBook
, you do not need to access variables through getters. Directly accessing them by their names@...
is less confusing and hence will likely decrease bugs.