On Tue, Mar 19, 2013 at 8:54 PM, Rotar D. <lists / ruby-forum.com> wrote:
> I do not know what is v ...
> I do not know Ruby (my languages are Haskell, C++ and Java), but my
> colleague showed this line of code for demonstrate "how to change the
> attributes of one object, based on the attributes of another one object
> on Ruby in one line"

# my version on one line:
v.attributes.each {|k,| v.send "#{k}=", info.instance_values[k]}

I still like it better.

> I asked him about complexity, and he said O(n^3)... So, I found it
> questionable.

Me, too.  I think it's O(n) in the number of attributes.  Let's do an
analysis based on what methods seem to do:

v.attributes
either O(1) if it just hands out a reference or O(n) if a Hash is
constructed for all attributes

.map {|k, val| {k => info.instance_values[k]}}
O(n) for every key value pair a new Hash with a single entry is created.

.reduce(:merge).
O(n) all single entry Hashes are merged into one and since Hash lookup
is O(1) we don't get higher than O(n)

each_pair{ |key, value| v.send( (key + '=').to_sym, value) }
O(n) unles v.send does something awfully complex.

So the maximum is O(n).  With small number of values which we likely
see here the performance is probably dominated by all the unnecessary
object constructions.  I assume that my suggestion (see above) is
faster but ultimately only measurement will show.

Does that help?

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/