NameError when using alias_method -- but method exists?

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 moduleYAML’
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

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

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

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