From: "Jesús Gabriel y Galán" Date: 2010-04-14T00:55:13+09:00 Subject: Re: scope limitation causes problems for me. On Tue, Apr 13, 2010 at 5:41 PM, 김준영 wrote: > hi, everyone. > > I have the following codes. > > m = Hash.new("merong") > > m["b"] = "babo" > > def func >    begin >        m["a"] = "merong" >    rescue => e >        puts e >    end > end > > func > > > > exception is > > undefined local variable or method `m' for main:Object > > Could I know the reason this was caused?? The reason is that the def keyword starts a new scope, and doesn't inherit the surrounding scope (it's not a closure). You could pass the hash as a parameter to the function for example: m = Hash.new("merong") m["b"] = "babo" def func hash begin hash["a"] = "merong" rescue => e puts e end end func m Or use define_method, if you really wanted a closure over that scope: m = Hash.new("merong") m["b"] = "babo" self.class.send (:define_method, :func) do begin m["a"] = "merong" rescue => e puts e end end func Jesus.