From: Erwin Date: 2008-09-08T17:34:01+09:00 Subject: Re: any way to write it in one line ? On 7 sep, 10:37, Robert Klemme wrote: > On 06.09.2008 20:17,Erwinwrote: > > >      works =  Array.new > >      session[:cart_works].each_pair {|key, value| works << key if > > value == user_id} > > > how to initialize the works array in the bloc ? > > You cannot do it inside the block because then it is not know after the > block because of how the scoping works: > > $ ruby -e '[1].each { w = 10 }; p w' > -e:1: undefined local variable or method `w' for main:Object (NameError) > > You can do > > works = session[:cart_works].inject [] do |w,(k,v)| >    w << k if v == user_id >    w > end > > works = session[:cart_works].select {|k,v| v == user_id}.map {|k,v| k} > > Kind regards > >         robert thanks Robert ! I have to look a little bit into the .select ...