ruby on rails - Can't extend core Class -
i'm trying extend core classes , have no idea why following fails when being called through rails 4 console:
# config/application.rb config.autoload_paths += dir["#{config.root}/lib/**/"] # lib/core_ext/string.rb class string def test self + " test" end end # lib/core_ext/array.rb class array def mean sum / size if sum && size end end # lib/core_ext/test.rb class test def test "some test" end end
rails console output:
1] pry(main)> test = test.new #<test:0x007ff63971b588> [2] pry(main)> test.test "some test" [3] pry(main)> "string".test nomethoderror: private method `test' called "string":string (pry):3:in `__pry__' [4] pry(main)> [1,2,3].mean nomethoderror: undefined method `mean' [1, 2, 3]:array (pry):4:in `__pry__'
you should add following line require files project
dir[file.join(rails.root, "lib", "core_ext", "*.rb")].each {|l| require l }
taken in ruby on rails, extend string class, should code put in?
Comments
Post a Comment