PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 25, 2022

[FIXED] How in Ruby can I get the module in which a module instance method is defined?

 November 25, 2022     module, ruby     No comments   

Issue

Assuming I have:

module X::Y::Z::M
  def foo
  end
end

How in foo can I dynamically determine the module X::Y::Z::M in the foo method? self.class will not work because it will evaluate to the class that includes the module.


Solution

You can use Module.nesting method which returns an array of nesting modules, Refer nesting method of Moudle

module X::Y::Z::M
  def foo
    Module.nesting.first
  end
end


Answered By - Supriya Medankar
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing