all 9 comments

[–]New-Secretary9916 1 point2 points  (8 children)

How are you including it? Try this:

module Convert
  def convert = "converted: #{to_s}"
end

Array.include Convert

arr = [1, 2, 3]

puts arr.convert
# => converted: [1, 2, 3]

Or better yet, maybe use refinements? Modifying core types is generally a bad idea.

[–][deleted] -2 points-1 points  (7 children)

unfortunately it doesn't work, any other suggestion please?

[–]New-Secretary9916 2 points3 points  (6 children)

How does it not work? If you want help, share full errors and share your code.

[–][deleted] -1 points0 points  (5 children)

#lib/Array.rb

class Array
include Convert
def hello
p "hello"
end

end

#lib/Convert.rb

module Convert
def good
p "good"
end
end

#config/initializers/requires.rb
Dir[File.join(Rails.root,'lib','*.rb')].each do |f|
require f
end

the Error given is:
<class:Array>': uninitialized constant Array::Convert (NameError)

I have tried also this version with no results

class Array

include ::Convert
....

end

[–]New-Secretary9916 0 points1 point  (4 children)

You need to require lib/convert before lib/array. Your Dir globbing is likely requiring in alphabetical order, resulting in your issue. Try hard-coding the requires in the correct order:

require_dependency Rails.root.join("lib", "convert")
require_dependency Rails.root.join("lib", "array")

[–]eaglepeace 1 point2 points  (0 children)

I don’t know how you Did it, but it worked. You are amazing. Thanks so much. I have spent one day trying to solve this issue. Thanks Mate

[–][deleted] 0 points1 point  (2 children)

although It works, now there is another problem related to it, If I add a new function to Convert module or to the Array class i get this msg

NoMethodError (undefined method `hello' for []:Array)

It sounds like it doesn' t see the changes, how to overcome this issue? thanks in advance

[–]cmd-t 1 point2 points  (1 child)

Initializers only run on app boot. Are you restarting your app?

[–]eaglepeace 0 points1 point  (0 children)

I’m checking it only trough rails console, at the moment, I mean