Skip to content

Instantly share code, notes, and snippets.

@misfo
Last active December 24, 2015 11:59
Show Gist options
  • Save misfo/6794668 to your computer and use it in GitHub Desktop.
Save misfo/6794668 to your computer and use it in GitHub Desktop.
module CallInstance
def call(*args, &block)
new(*args, &block).call
end
alias_method :[], :call
end
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