From: Todd Benson Date: 2007-08-30T23:22:35+09:00 Subject: Re: How to make an array of hashes to a single array with all the values of these hashes ? On 8/30/07, kazaam wrote: > thanks guys! dimas solution showed me my error > > myvalue.each {|arr| @my_new_array << arr.values } > puts @my_new_array > > is working if I initialize @my_new_array = [] before like dima did with a = [] but two questions: > > Why is a local variable my_new_array not working but just @ instance or $ global variables and why do I have > initialize this varriable? I think you don't need this in ruby? But without initialisation it just shows nil? It's about scope. I'm sure the veterans here can give you a thorough explanation, (and please pipe in here, because I don't want to spread disinformation), but basically the block tries to retain its own scope as best it can (from what I understand). So, new variables that are not "swallowed" from the scope outside of the block need to be initialized, and those initialized within the block don't get outside of the block (what happens in Vegas stays in Vegas, blah blah). Simple example... irb(main):001:0> a = 1,2,3,4 => [1, 2, 3, 4] irb(main):002:0> a.each { |i| b = i } #nothing happens here outside of the block for b => [1, 2, 3, 4] irb(main):003:0> b NameError: undefined local variable or method `b' for main:object Here's one that will make you think a little that is somewhat OT, but demonstrates namespace danger... irb(main):001:0> a = 1,2,3,4 => [1, 2, 3, 4] irb(main):002:0> a.inject{ |s, a| s + a } => 10 irb(main):003:0> a => 4 The a is not the accumulator, but it has changed even though the method doesn't have a ! following it. These are the small idiosyncrasies that we have to be aware of with namespace and scope. I may not be understanding your question, though. > kazaam Todd