Topic: reusing blocks

hey all,

i have this piece of code to access an hash

current_order.find{ |x| x[:touched_at].to_i == item.last[:touched_at].to_i }[:quantity] = item.last[:quantity]
current_order.find{ |x| x[:touched_at].to_i == item.last[:touched_at].to_i }[:size_id] = item.last[:size_id]
current_order.find{ |x| x[:touched_at].to_i == item.last[:touched_at].to_i }[:color_id] = item.last[:color_id]  

I would like to simplify it with reusing the block.

This isnt working. It will return a different order_item while the upper code is working 100% but i think it is ugly.

find_proc = Proc.new{ |x| x[:touched_at].to_i == item.last[:touched_at].to_i }
current_order.find{ find_proc}[:quantity] = item.last[:quantity]
current_order.find{ find_proc }[:size_id] = item.last[:size_id]
current_order.find{ find_proc }[:color_id] = item.last[:color_id]  

Does somebody know what i am doing wrong? I am quite sure that i cant use a Proc here. I am kind of a newbie in Ruby meta programming. Can i use a Proc here? The result is in a loop and i think the find_proc variable is Static for a reason because he find the same order_item each time.

Thanks in advanced!

Last edited by mokkol (2010-05-31 09:14:19)

Re: reusing blocks

what is current_order?  An array?  A single object?  What?  Also, what is x?  Don't use x as a variable name, use something descriptive.  What's this? "item.last[:touched_at].to_i"

Generally speaking, what's going on here?

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams