# Undifined local variable or method error

**URL:** <https://www.ruby-forum.com/t/undifined-local-variable-or-method-error/187128>\
**Category:** Ruby\
**Created:** [April 17, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/undifined-local-variable-or-method-error/187128 "2010-04-17T19:30:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![simbolla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@simbolla](https://www.ruby-forum.com/u/simbolla)\
**Post date:** [April 17, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/undifined-local-variable-or-method-error/187128/1 "2010-04-17T19:30:18Z")

</div>

Hi all,

Below is my code

h={}  
arr=[]  
def gethash  
h[1]=0  
return h  
end  
arr[0]=gethash  
puts arr[0].each {|k,v| puts “key #{k} value #{v}”}

For the above i get error saying “Undifined local variable or method ‘h’  
for main:Object(NameError)”

may i know reason for this error?

---

<div class="post-metadata">

**Author:** ![simbolla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@simbolla](https://www.ruby-forum.com/u/simbolla)\
**Post date:** [April 17, 2010, 8:27pm UTC](https://www.ruby-forum.com/t/undifined-local-variable-or-method-error/187128/2 "2010-04-17T20:27:38Z")

</div>

Hi –

On Sun, 18 Apr 2010, Simbolla S. wrote:

> arr[0]=gethash  
> puts arr[0].each {|k,v| puts “key #{k} value #{v}”}
> 
> For the above i get error saying “Undifined local variable or method ‘h’  
> for main:Object(NameError)”
> 
> may i know reason for this error?

The def keyword starts a new local scope. The same happens with class:

a = 1  
class C  
puts a # new local scope, so a is not defined  
end

David

–  
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally  
COMPLEAT Coming to Chicago area, June 18-19, 2010!  
RUBYIST [http://www.compleatrubyist.com](http://www.compleatrubyist.com)

---

<div class="post-metadata">

**Author:** ![simbolla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@simbolla](https://www.ruby-forum.com/u/simbolla)\
**Post date:** [April 17, 2010, 7:58pm UTC](https://www.ruby-forum.com/t/undifined-local-variable-or-method-error/187128/3 "2010-04-17T19:58:58Z")

</div>

On Sat, Apr 17, 2010 at 7:30 PM, Simbolla S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> arr[0]=gethash  
> puts arr[0].each {|k,v| puts “key #{k} value #{v}”}

to my dismay  
def xxx… end  
is _not_ a closure  
while  
define\_method :xxx is  
IOW  
h is just not visible in def while it would if you define gethas via  
define\_method

be careful though on the toplevel you cannot use define\_method, do  
something like  
include( Module::new do  
h = {}  
define\_method :xxx do h.tap{ |hh| h[1] = 0 } end  
end )

puts xxx  
xxx[:a] = 42  
puts xxx

oh yes this code is ugly, but I wanted to demonstrate the semantics…  
HTH  
R.

---

<div class="post-metadata">

**Author:** ![simbolla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@simbolla](https://www.ruby-forum.com/u/simbolla)\
**Post date:** [April 18, 2010, 4:05am UTC](https://www.ruby-forum.com/t/undifined-local-variable-or-method-error/187128/4 "2010-04-18T04:05:15Z")

</div>

On Apr 17, 11:30 am, Simbolla S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> for main:Object(NameError)"  
> If you want h to be a global variable, accessible everywhere, prefix  
> it with a dollar sign:

$h={}  
def foo  
$h[1]=0  
$h  
end  
foo  
p $h  
#=\> {1=\>0}

---

<div class="post-metadata">

**Author:** ![simbolla](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@simbolla](https://www.ruby-forum.com/u/simbolla)\
**Post date:** [April 18, 2010, 10:57am UTC](https://www.ruby-forum.com/t/undifined-local-variable-or-method-error/187128/5 "2010-04-18T10:57:59Z")

</div>

Gavin K. wrote:

> On Apr 17, 11:30ï¿½am, Simbolla S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > for main:Object(NameError)"  
> > If you want h to be a global variable, accessible everywhere, prefix  
> > it with a dollar sign:
> 
> $h={}  
> def foo  
> $h[1]=0  
> $h  
> end  
> foo  
> p $h  
> #=\> {1=\>0}

But usually it would be better instead to:

(1) pass h as an argument to method foo; or  
(2) define a class with instance variable @h
