Last active
December 24, 2015 11:59
-
-
Save misfo/6794668 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module CallInstance | |
def call(*args, &block) | |
new(*args, &block).call | |
end | |
alias_method :[], :call | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SiblingModuleName | |
extend CallInstance | |
def initialize(module_name, sibling_name) | |
@module_name = module_name | |
@sibling_name = sibling_name | |
end | |
def call | |
parent = parent_module_name() | |
if parent | |
"#{parent}::#{@sibling_name}" | |
else | |
@sibling_name | |
end | |
end | |
private | |
def parent_module_name | |
@module_name[/\A(.+)::[^:]+\z/, 1] | |
end | |
end | |
p SiblingModuleName['Net::HTTP', 'FTP'] #=> "Net::FTP" | |
p SiblingModuleName['Net::HTTP::Get', 'Post'] #=> "Net::HTTP::Post" | |
p SiblingModuleName['Object', 'String'] #=> "String" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment