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 >> endBut 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jan 18, 5:36 am, Old Echo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---