I don't understand "inject" in the Refactoring #exhibit code.

36 views
Skip to first unread message

Jordan Nelson

unread,
Aug 11, 2015, 8:33:53 PM8/11/15
to Objects on Rails
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



Sammy Larbi

unread,
Aug 12, 2015, 10:00:28 AM8/12/15
to objects-...@googlegroups.com
On Tue, Aug 11, 2015 at 7:33 PM, Jordan Nelson <jordan.do...@gmail.com> wrote:
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?  


Yes, that's exactly what's supposed to happen. =)

Regards,
Sam



 
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



--
You received this message because you are subscribed to the Google Groups "Objects on Rails" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objects-on-rai...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jordan Nelson

unread,
Aug 13, 2015, 9:09:52 PM8/13/15
to Objects on Rails
Thank you, Sam.

-J
Reply all
Reply to author
Forward
0 new messages