Suganya
Suganya

Reputation: 741

Rails - List of Files inside a folder

I have a module and which have some files under the module

module Man

which has some five files called

module man
   module head
      def a
      end
   end
end

module man
   module hand
      def a
      end
   end
end

I need to access list of sub-modules that are under the module 'man' and also I need to access the list of methods in each sub-modules.

I tried doing this

      array_notification_classes = Dir.entries("app/models/notifications").select {|f| !File.directory? f}

But it returned a list of submodules which is a string.

 array_notification_classes  = ["head.rb", "hand.rb"]

From now how should I get the list of method names from each sub-module?

Upvotes: 0

Views: 278

Answers (2)

Suganya
Suganya

Reputation: 741

My array_notification_classes = ["head.rb", "hand.rb"]        

 array_notification_classes.each do |notification_class|
    notification_class = "Notifications::#{notification_class[0..-4].classify}".constantize
    notification_class_methods = notification_class.instance_methods
 end

This returned all the instance methods in Notifications::Head, notifications::Hand

Upvotes: 0

blelump
blelump

Reputation: 3243

Having an array of file names, e.g. array_notification_classes = ["head.rb", "hand.rb"]

array_notification_classes.each do |file_name|
  require file_name
  include file_name.split(".").first.classify.constantize
end

or into a class:

class Notification
end

array_notification_classes.each do |file_name|
  require file_name
  Notification.class_eval do
    include file_name.split(".").first.classify.constantize
  end
end

Upvotes: 1

Related Questions