Return-Path: Delivered-To: apmail-incubator-deltacloud-dev-archive@minotaur.apache.org Received: (qmail 58080 invoked from network); 31 Jan 2011 16:31:45 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 31 Jan 2011 16:31:45 -0000 Received: (qmail 24086 invoked by uid 500); 31 Jan 2011 16:31:45 -0000 Delivered-To: apmail-incubator-deltacloud-dev-archive@incubator.apache.org Received: (qmail 23983 invoked by uid 500); 31 Jan 2011 16:31:43 -0000 Mailing-List: contact deltacloud-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: deltacloud-dev@incubator.apache.org Delivered-To: mailing list deltacloud-dev@incubator.apache.org Received: (qmail 23974 invoked by uid 99); 31 Jan 2011 16:31:42 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 31 Jan 2011 16:31:42 +0000 X-ASF-Spam-Status: No, hits=-5.0 required=5.0 tests=RCVD_IN_DNSWL_HI,SPF_HELO_PASS,SPF_PASS,T_FRT_BELOW2 X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of mfojtik@redhat.com designates 209.132.183.28 as permitted sender) Received: from [209.132.183.28] (HELO mx1.redhat.com) (209.132.183.28) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 31 Jan 2011 16:31:36 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p0VGVEEr019039 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 31 Jan 2011 11:31:14 -0500 Received: from redhat.com (dhcp-2-138.brq.redhat.com [10.34.2.138]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p0VGVAZv026717 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 31 Jan 2011 11:31:12 -0500 Date: Mon, 31 Jan 2011 17:31:10 +0100 From: Michal Fojtik To: deltacloud-dev@incubator.apache.org Subject: Re: [PATCH] Introduces blob metadata - Adds ability to specify arbitrary user metadata (KEY-VALUE) for S3, Cloudfiles and Azure, during blob creation Message-ID: <20110131163110.GC5879@redhat.com> References: <1296238506-5650-1-git-send-email-marios@redhat.com> <1296238506-5650-2-git-send-email-marios@redhat.com> <20110131092805.GB5879@redhat.com> <4D46E0C1.4040406@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <4D46E0C1.4040406@redhat.com> X-Operating-System: Linux patashnik 2.6.35.10-74.fc14.i686.PAE User-Agent: Mutt/1.5.20 (2009-06-14) X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-Virus-Checked: Checked by ClamAV on apache.org On 31/01/11 18:18 +0200, marios@redhat.com wrote: >Hi Michal, thanks for taking the time. > >I am confused by what the code fragment below does exactly. I *think* >i understand what .inject does, but the code below doesn't make sense >to me (or to irb it seems :) ) ; where does r come from? (I tried irb >and ruby debugger cos I thought I was going crazy but the code >doesn't work - i even thought it was a ruby version issue so i tried >1.8.6 AND 1.9.2 as I have come across a few 1.8/1.9 issues). > >Can you please explain in words what: > >meta_array.inject({}) { |user_meta, v| user_meta[v.first]=v.last ; r } This will create a new Hash ({} is an alias for Hash.new), then it will start to iterate through meta_array. Then user_meta is variable which holds that Hash and in 'v' is stored actual item from 'meta_array'. Then user_meta[v.first]=v.last create a new key using first item in array and store value from second item. Then I made a *typo*, 'r' variable should be 'user_meta', so you return this Hash and iterate to next item... I'm very sorry for that typo, it makes all this very confusing. Very good example of how to use .inject can be found here: http://blog.jayfields.com/2008/03/ruby-inject.html >does? (e.g. take the meta_array two dimensional array and ... iterate >over the user_meta array etc etc). Out of curiosity where are you >getting 'ruby best practice' from (e.g. can you recommend a good >book/site/blog etc?) Yes, actually it's http://rubybestpractices.com ;-) There is an article about functional programming, which describes this. Anyway, it's not a blocker, just small hint (I found myself .inject very useful when I was doing some meta programming in client library. It saves variables and lines of code (and it's better for garbage collecting)). -- Michal >On 31/01/11 11:28, Michal Fojtik wrote: >>On 28/01/11 20:15 +0200, marios@redhat.com wrote: >> >>Code looks good, applied cleanly, Cucumber is happy, ACK. >>A small, minor inline comment bellow. >> >>-- Michal >> >>>From: marios > >>>> max.to_i.times do |i| could be used here as well (but do the same job ;-) >> >>>+ key = params[:"meta_name#{i}"] >>>+ key = "HTTP_X_Deltacloud_Blobmeta_#{key}" >>>+ value = params[:"meta_value#{i}"] >>>+ user_meta[key] = value >>>+ end #max.each do >>>+ else #can try to get blob_metadata from http headers >>>+ meta_array = request.env.select{|k,v| >>>k.match(/^HTTP[-_]X[-_]Deltacloud[-_]Blobmeta[-_]/i)} >>>+ meta_array.each do |k,v| >>>+ user_meta[k] = v >>>+ end >> >>meta_array.inject({}) { |user_meta, v| user_meta[v.first]=v.last ; r } >> >>(same as above ;-) But it's a good Ruby practive to use inject if it's >>possible. >> >> -- -------------------------------------------------------- Michal Fojtik, mfojtik@redhat.com Deltacloud API: http://deltacloud.org --------------------------------------------------------