Ruby does not have an import system like python, so variables and methods at the top level like REPO_TYPES
and temp_dir
are effectively global variables and methods.
Use modules to aggressively namespace, even for your main
, especially when a small script begins to span more than one file:
module Degit
def self.main # define singleton method
end
end
Degit.main # call singleton method
This is also true for methods as well. def self.main
in the example defines a singleton method on Degit
itself. (Degit
is a singleton in the sense that it will be the only instance of Module
named "Degit", and main
is a method it will now have).
Ruby classes operate in the same way:
class Foo
class << self # opens singleton context
def foo # also defines a singleton method
end
end
end
On another note, I feel like RepoType
should either be:
- completely removed and its responsibilities handled by
Repo
Or
- named
Host
and be more cohesive by owning variables and methods REPO_TYPE
and find_repo_type
within it, along with the regex definitions associated with each Host
Here's an example combining what I've outlined above:
class Host
class << self
def all
@all ||= {} # the `Host` singleton is an instance of `Class`, and can have @vars!
end
def ssh_regex(hostname)
/blahblah#{hostname}blahblah/
end
end
def initialize(name, hostname)
@name = name
@host_uri = URI("http://www.#{hostname}")
end
end
```