Metode dinamis Impor Ruby Rhinement

module DefineMethodModule
  5.times do |i|
    # Methods defined with `define_method` or `define_singleton_method`
    # do not count as methods defined in Ruby code.
    #
    # As such they can't be imported in Refinements!
    define_method :"define_method_#{i}" do
      puts "its me, the new define_method_#{i}!"
    end
  end
end

module StringEvalModule
  5.times do |i|
    # The only way to import dynamically generated methods
    # to Refinements is to construct a String with Ruby code
    # that defines the method and pass it to `eval` or `instance_eval`
    eval <<~RUBY
    	def string_eval_#{i}
        	puts "its me, the new string_eval_#{i}!"
        end
    RUBY
  end
end

module SomeRefinement
  refine String do
    # import_methods DefineMethodModule
    # the line above would result in an error
    #=> in `import_methods': Can't import method which is not defined with Ruby code
    import_methods StringEvalModule
  end
end

using SomeRefinement
string = ""
string.string_eval_3 #=> its me, the new string_eval_3!
Mateusz Drewniak