Last active
August 29, 2015 14:05
-
-
Save domgetter/1ec591b5b73208f4a174 to your computer and use it in GitHub Desktop.
How to share methods between modules while maintaining best namespacing practices.
This file contains hidden or 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 GemName | |
module CoolClassInterface | |
module Win32 | |
def func_a; end | |
def func_b; end | |
end | |
module Linux | |
def func_a; end # but now I don't have DRY code. How do I make Win32 and Linux share this? | |
def func_c; end | |
end | |
end | |
class CoolClass | |
include CoolClassInterface::Win32 if OS.windows? | |
include CoolClassInterface::Linux if OS.linux? | |
end | |
end | |
class_member = CoolClass.new # can definitely use func_a |
This file contains hidden or 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 GemName | |
module CoolClassInterface | |
def func_a; end | |
end | |
module Win32 | |
# I get the functionality I want, but I lose namespacing. | |
# I'd like to refer to CoolClassInterface::Win32. Is that | |
# even something I should want? | |
include CoolClassInterface | |
def func_b; end | |
end | |
module Linux | |
include CoolClassInterface | |
def func_c; end | |
end | |
class CoolClass | |
include Win32 if OS.windows? | |
include Linux if OS.linux? | |
end | |
end | |
class_member = CoolClass.new # can definitely use func_a |
This file contains hidden or 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 GemName | |
module CoolClassInterface | |
def func_a; end | |
module Win32 | |
# I get the namespacing I want, but this seems bulky? | |
# is including a module while nested inside that module | |
# considered bad practice? | |
include CoolClassInterface | |
def func_b; end | |
end | |
module Linux | |
include CoolClassInterface | |
def func_c; end | |
end | |
end | |
class CoolClass | |
include CoolClassInterface::Win32 if OS.windows? | |
include CoolClassInterface::Linux if OS.linux? | |
end | |
end | |
class_member = CoolClass.new # can definitely use func_a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment