<<红宝石中的运算符 [英] << operator in ruby
问题描述
我对红宝石很陌生,
我在rails中遇到了以下代码,但是我不知道<<"如何.运算符的工作原理及其在以下代码中的作用
I came across the following code in rails, but I don't know how the "<<" operator works and what it does in the below code
def <<( rate )
r = Rating.new
r.rate = rate
r.rateable = proxy_owner
...
...
end
class << ActiveRecord::Base
...
...
end
有人可以向我解释吗?
,这是代码 https ://github.com/azabaj/acts_as_rateable/blob/master/lib/acts_as_rateable.rb
推荐答案
def <<( rating )
:
在您的示例中,这用于向可评估的模型添加评估. (例如, acts_as_rateable.rb:41 ),类似将某些内容附加到字符串(str << "abc"
).由于它在模块中,因此仅会包含在您声明为可评估的模型中.
def <<( rating )
:
In your example, this is used to add a rating to a rateable model. (E.g. in acts_as_rateable.rb:41), similar to appending something to a string (str << "abc"
). As it is within a module, it will only be included for the models that you declare as rateable.
class << ClassName
:
All the methods inside of this block will be static / class methods (see this blog entry). (In this case, all the models will have the methods Model.example_static_method.)
这篇关于<<红宝石中的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!