<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in 'Trying to understand a code example (pg 110, cart.rb)' | Pragmatic Forums</title>
    <link>http://forums.pragprog.com/forums/66/topics/416</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <description></description>
    <item>
      <title>Trying to understand a code example (pg 110, cart.rb) posted by Scott Gardner @ Fri, 09 May 2008 14:53:41 -0000</title>
      <description>&lt;p&gt;That &lt;strong&gt;really&lt;/strong&gt; helped, thanks!&lt;/p&gt;</description>
      <pubDate>Fri, 09 May 2008 14:53:41 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:66:416:2761</guid>
      <author>Scott Gardner</author>
      <link>http://forums.pragprog.com/forums/66/topics/416</link>
    </item>
    <item>
      <title>Trying to understand a code example (pg 110, cart.rb) posted by Sam Ruby @ Fri, 09 May 2008 13:51:53 -0000</title>
      <description>&lt;p&gt;The block is automatically converted to a Proc, which is an object.&lt;/p&gt;


	&lt;p&gt;It may help to play with this yourself by adding your own find method to the Array class, thus:&lt;/p&gt;


&lt;pre&gt;
class Array
  def my_find &amp;#38;block
    puts "block class = #{block.class}" 
    for item in self
      puts "testing item: #{item}" 
      if block.call(item) == true
        return item
      end
    end
    return false
  end
end

data = [1, 2, 3, 4, 5]

puts "scanning for a three" 
result = data.my_find {|i| i == 3}
puts "result: #{result}" 
&lt;/pre&gt;</description>
      <pubDate>Fri, 09 May 2008 13:51:53 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:66:416:2758</guid>
      <author>Sam Ruby</author>
      <link>http://forums.pragprog.com/forums/66/topics/416</link>
    </item>
    <item>
      <title>Trying to understand a code example (pg 110, cart.rb) posted by Scott Gardner @ Fri, 09 May 2008 12:50:23 -0000</title>
      <description>&lt;p&gt;Thanks for the detailed explanation. I am really just trying to put a handle on this because I realize it&amp;#8217;s a key and common usage in Ruby/Rails. I think, for now, I will just have to rely on rote memory of how this works.&lt;/p&gt;


	&lt;p&gt;Also, can you confirm/correct your statement that this block is in fact an object? According to &lt;a href="http://rubylearning.com/satishtalim/ruby_blocks_and_procs.html"&gt;this explanation on rubylearning.com&lt;/a&gt;, a block is not an object unless it is converted to a Proc (e.g., using lambda).&lt;/p&gt;</description>
      <pubDate>Fri, 09 May 2008 12:50:23 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:66:416:2757</guid>
      <author>Scott Gardner</author>
      <link>http://forums.pragprog.com/forums/66/topics/416</link>
    </item>
    <item>
      <title>Trying to understand a code example (pg 110, cart.rb) posted by Sam Ruby @ Fri, 09 May 2008 01:32:01 -0000</title>
      <description>&lt;blockquote&gt;
	&lt;p&gt;1. @items is initialized as an empty array&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;Note that this only occurs when the Cart itself is initialized.  After that point, @items can change.&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;2. add_product receives a product object&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;Yes.&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;3. The @items array is passed to the block&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;More precisely, each item in the @items array is passed to the block until an item is found for which the block evaluates to true.&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;4. Each @items array item is checked &amp;#8230;&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;This is correct.&lt;/p&gt;


&lt;blockquote&gt;
	&lt;p&gt;I still do not understand how the block &amp;#8230; knows to return the actual item object&lt;/p&gt;

&lt;/blockquote&gt;




	&lt;p&gt;It doesn&amp;#8217;t.&lt;/p&gt;


	&lt;p&gt;The block is a procedure.  It is passed as a parameter to the find method.  It is an object with a call method.  This means that the caller (in this case, the find method) can call it as many times at it likes, with whatever parameters it likes.  And that is exactly what the find method does.  It calls the block repeatedly with each successive item in the array until it finds one which causes the block to evaluate true.  If this occurs, the find method knows which item it passed to the block, and it simply returns that item.  If, instead, it exhausts the list, it simply returns nil instead.&lt;/p&gt;</description>
      <pubDate>Fri, 09 May 2008 01:32:01 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:66:416:2753</guid>
      <author>Sam Ruby</author>
      <link>http://forums.pragprog.com/forums/66/topics/416</link>
    </item>
    <item>
      <title>Trying to understand a code example (pg 110, cart.rb) posted by Scott Gardner @ Thu, 08 May 2008 23:58:47 -0000</title>
      <description>&lt;p&gt;The following section of code (excerpted from page 110 depot_g/app/models/cart.rb line 2 in the book, but I&amp;#8217;ve included relevant setup code) has_thrown_me_for :a =&amp;gt; :loop&amp;#8230;&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class Cart
    attr_reader :items

    def initialize
        @items = []
    end

    def add_product(product)
        current_item = @items.find {|item| item.product == product}  # say what?!?
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;Here&amp;#8217;s how I understand it:&lt;br /&gt;1. @items is initialized as an empty array&lt;br /&gt;2. add_product receives a product object&lt;br /&gt;3. The @items array is passed to the block&lt;br /&gt;4. Each @items array item is checked to see if its product.id equals the product.id of the product passed to add_product, and if so that matched item is returned to find, which then returns that item to be assigned to current_item&lt;/p&gt;


	&lt;p&gt;Can someone please confirm that I have this correct? Presuming yes, I still do not understand how the block, which resolves true or false, knows to return the actual item object when a true condition occurs.&lt;/p&gt;


	&lt;p&gt;Thanks!&lt;/p&gt;</description>
      <pubDate>Thu, 08 May 2008 23:58:47 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:66:416:2752</guid>
      <author>Scott Gardner</author>
      <link>http://forums.pragprog.com/forums/66/topics/416</link>
    </item>
  </channel>
</rss>
