Hi,
I do not understand the def self.exhibit(object, context) method in this tutorial. I do not understand why inject is called on the array. In my mind, the way #inject works is a that the first block argument works as an accumulator.
Lets say the first element is_applicable. Then exhibit_if_applicable instantiates a TextPostExhibit and that becomes the new value of the accumulator (i.e. object) in the next iteration of the loop. This makes sense, because if the loop runs and nothing else is applicable, then you end up with an exhibit. If nothing is applicable at all you end up with an object like Post.
I guess the problem for me is if the exhibit_if_applicable returns an exhibit twice or three times it seems like then you would start to have exhibits within exhibits. Is this what is supposed to happen? Am I missing something?
def self.exhibits
[
TextPostExhibit,
PicturePostExhibit,
LinkExhibit
]
end
def self.exhibit(object, context)
exhibits.inject(object) do |object, exhibit|
exhibit.exhibit_if_applicable(object, context)
end
end
def self.exhibit_if_applicable(object, context)
if applicable_to?(object)
new(object, context)
else
object
end
end