On 31/01/11 18:18 +0200, [email protected] 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, [email protected] wrote:
Code looks good, applied cleanly, Cucumber is happy, ACK.
A small, minor inline comment bellow.
-- Michal
From: marios <[email protected]>
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, [email protected]
Deltacloud API: http://deltacloud.org
--------------------------------------------------------