0

I have a ruby script lib/tasks/material_parser_helper.rb

and a module lib/common_parser_methods.rb

How could I extends all the module methods into 'material_parser_helper.rb'

Therefore, I can call the clean_key in any methods of MaterialParserHelper

If I tried to extend CommonParserMethods after the class,

I will got the error NameError: uninitialized constant SummaryProjection::CommonMethods

class MaterialParserHelper
  extend CommonParserMethods

  def   test
      clean_key("jckdj")
  end

end

common_parser_methods.rb

module CommonParserMethods
  def clean_key(key)
    return key.strip.squish.gsub(/\s/, '_').gsub('-', '_').downcase
  end
end

Application.rb

config.autoload_paths += %W(#{config.root}/lib/ #{config.root}/lib/common_parser_methods.rb #{config.root}/lib/tasks)
user3675188
  • 7,271
  • 11
  • 40
  • 76
  • First of all your application.rb is wrong, the brackets are incomplete. – Almaron Mar 27 '15 at 05:48
  • @Almaron Hi I didn't found where does the cinomeplete happens , I think the syntax is right, otherwise the Rails should give me error. https://github.com/shail85/fakesbook/blob/1ff72596de8cd3ec200570bde2077f2dfa314828/config/initializers/file – user3675188 Mar 27 '15 at 05:58
  • Your closing bracket is commented out. It should be `config.autoload_paths += %W(#{config.root}/lib/) #comment` – Almaron Mar 27 '15 at 11:37

1 Answers1

2

It looks like you want to include CommonParserMethods, not extend your class with it. This will give instances of MaterialParserHelper the methods defined in CommonParserMethods.

More info on include vs. extend here: What is the difference between include and extend in Ruby?

That said, your error message references some code that is not shown; a class called SummaryProjection. Are you sure that the MaterialParserMethods class is causing this problem?

Community
  • 1
  • 1
messanjah
  • 8,977
  • 4
  • 27
  • 40