I am quite new to ruby,
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
Can anybody explain to me?
Edit: here is the code https://github.com/azabaj/acts_as_rateable/blob/master/lib/acts_as_rateable.rb
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.)