# NameError when using alias\_method -- but method exists?

**URL:** <https://www.ruby-forum.com/t/nameerror-when-using-alias-method-but-method-exists/126406>\
**Category:** Rails\
**Created:** [January 18, 2008, 1:36am UTC](https://www.ruby-forum.com/t/nameerror-when-using-alias-method-but-method-exists/126406 "2008-01-18T01:36:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [January 18, 2008, 1:36am UTC](https://www.ruby-forum.com/t/nameerror-when-using-alias-method-but-method-exists/126406/1 "2008-01-18T01:36:38Z")

</div>

Hello Rubyists,  
I am a bit stumped here. I want to extend the ‘load\_file’ method in the  
YAML module. Following along with the PickAxe example of making old  
methods do new things, I try this in irb:

> > module YAML  
> > alias\_method :orig\_load\_f, :load\_file  
> > def load\_file(\*args)  
> > contents = orig\_load\_f(\*args)  
> > contents.symbolize\_keys.each\_value {|v| v.symbolize\_keys }  
> > end  
> > end

But after that last ‘end’, irb explodes, saying:

NameError: undefined method `load_file' for module`YAML’  
from (irb):2:in `alias\_method’  
from (irb):2

But, continuing in irb:

> > #But apparently that NameError is not true?  
> > ?\> YAML.load\_file(‘test.yml’)  
> > =\> {“node\_three”=\>{“a”=\>1, “b”=\>2, “c”=\>3}, “node\_one”=\>{“a”=\>1, “b”=\>2,  
> > “c”=\>3}  
> > , “node\_two”=\>{“a”=\>1, “b”=\>2, “c”=\>3}}
> 
> >

Am I missing something or taking the wrong approach? Any input would be  
much appreciated.

Thanks,  
kodama

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [January 18, 2008, 1:05pm UTC](https://www.ruby-forum.com/t/nameerror-when-using-alias-method-but-method-exists/126406/2 "2008-01-18T13:05:04Z")

</div>

Kodama,

load\_file is a “module method” of YAML module, not a “module instance  
method”. alias\_method works with module instance methods (as it  
seems). Try running the below code, to understand the differences

require ‘yaml’  
module YAML  
def load\_file1(\*args)  
end  
def YAML.load\_file2(\*args)  
end  
end  
module YAML  
alias\_method :orig\_load\_f1, :load\_file1  
alias\_method :orig\_load\_f2, :load\_file2  
end

–sujoy

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [January 18, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/nameerror-when-using-alias-method-but-method-exists/126406/3 "2008-01-18T13:27:29Z")

</div>

Kodama,

load\_file is a “module method” of YAML module, not a “module instance  
method”. alias\_method works with module instance methods (as it  
seems). Try running the below code, to understand the differences

require ‘yaml’  
module YAML  
def load\_file1(\*args)  
end  
def YAML.load\_file2(\*args)  
end  
end  
module YAML  
alias\_method :orig\_load\_f1, :load\_file1  
alias\_method :orig\_load\_f2, :load\_file2  
end

–sujoy

---

<div class="post-metadata">

**Author:** ![kodama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/439d5e/32.png) [@kodama](https://www.ruby-forum.com/u/kodama)\
**Post date:** [January 18, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/nameerror-when-using-alias-method-but-method-exists/126406/4 "2008-01-18T13:37:31Z")

</div>

Kodama,

load\_file is a “module method” of YAML module, not a “module instance  
method”. alias\_method works with module instance methods (as it  
seems). Try running the below code, to understand the differences

require ‘yaml’  
module YAML  
def load\_file1(\*args)  
end  
def YAML.load\_file2(\*args)  
end  
end  
module YAML  
alias\_method :orig\_load\_f1, :load\_file1  
alias\_method :orig\_load\_f2, :load\_file2  
end

–sujoy
