# Scope limitation causes problems for me

**URL:** <https://www.ruby-forum.com/t/scope-limitation-causes-problems-for-me/186871>\
**Category:** Ruby\
**Created:** [April 13, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/scope-limitation-causes-problems-for-me/186871 "2010-04-13T17:41:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSSS\_SSSSSS\_SSSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@SSSS\_SSSSSS\_SSSSSSS](https://www.ruby-forum.com/u/SSSS_SSSSSS_SSSSSSS)\
**Post date:** [April 13, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/scope-limitation-causes-problems-for-me/186871/1 "2010-04-13T17:41:58Z")

</div>

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??

---

<div class="post-metadata">

**Author:** ![SSSS\_SSSSSS\_SSSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@SSSS\_SSSSSS\_SSSSSSS](https://www.ruby-forum.com/u/SSSS_SSSSSS_SSSSSSS)\
**Post date:** [April 13, 2010, 5:49pm UTC](https://www.ruby-forum.com/t/scope-limitation-causes-problems-for-me/186871/2 "2010-04-13T17:49:35Z")

</div>

You either need to pass m as a parameter

def func(m)  
begin  
m[“a”] = “merong”  
rescue =\> e  
puts e  
end  
end

and call it like

func(m)

or make m global

$m = Hash.new(“merong”)

$m[“b”] = “babo”

def func  
begin  
$m[“a”] = “merong”  
rescue =\> e  
puts e  
end  
end

func

I recommend the first way

---

<div class="post-metadata">

**Author:** ![SSSS\_SSSSSS\_SSSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@SSSS\_SSSSSS\_SSSSSSS](https://www.ruby-forum.com/u/SSSS_SSSSSS_SSSSSSS)\
**Post date:** [April 13, 2010, 5:56pm UTC](https://www.ruby-forum.com/t/scope-limitation-causes-problems-for-me/186871/3 "2010-04-13T17:56:02Z")

</div>

On Tue, Apr 13, 2010 at 5:41 PM, ê¹€ì¤€ì˜ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â Â Â Â m[“a”] = “merong”
> 
> 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.

---

<div class="post-metadata">

**Author:** ![SSSS\_SSSSSS\_SSSSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/71e660/32.png) [@SSSS\_SSSSSS\_SSSSSSS](https://www.ruby-forum.com/u/SSSS_SSSSSS_SSSSSSS)\
**Post date:** [April 13, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/scope-limitation-causes-problems-for-me/186871/4 "2010-04-13T18:01:54Z")

</div>

On Apr 13, 2010, at 8:41 AM, ±èÁØ¿µ wrote:

> ```
> m["a"] = "merong"
> 
> ```
> 
> undefined local variable or method `m’ for main:Object
> 
> Could I know the reason this was caused??

In your code, m is defined as a local variable in the top scope, so it  
is not visible in methods. A straightforward fix would be to use $m  
instead of m, which will designate a global variable. However, I would  
strongly recommend to wrap your code in a class and have an instance  
variable @m which will be visible to every method of that class, thus  
avoiding evil global variables:

class Performer  
def initialize(default\_value)  
@m = Hash.new(default\_value)  
@m[“b”] = “babo”  
end

def func  
begin  
@m[“a”] = “merong”  
rescue =\> e  
puts e  
end  
end  
end

Then you can call it as:

Performer.new(“merong”).func

Gennady.
