I have a class that will check a users environment to find out if they have python
installed, the classes purpose is to check if the user has multiple versions of python installed and to not default to three:
class CheckForPythonicVariables
class << self
def find_python_env_var
py_path = [] # Results of the python env variables
env_vars = ENV.to_h
items = env_vars["Path"].split(";") # Split the environment variables into an array
items.each { |var|
if var.to_s.include?("Python") # Do you have python?
py_path.push(var)
end
}
py_path.each { |python|
if python.include?("Python27") # Python 2.7.x?
return true
elsif python.include?("Python3") # Python 3.x.x?
return false
else
raise "You do not have python installed"
end
}
end
end
end
Now this works, but it only works on Windows and a select few Linux OS
, apparently Parrot
is not one of them. Is there a way I can #split()
the environment variables by anything that is not a letter? For example:
Windows env var: C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Oracle\Java\javapath
Parrot OS env var: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Note how the variables are split by either a semi-colon(;
) or a colon(:
), is there a way I could use the #split()
function in ruby and split by anything that is not alphanumerical characters, or number? Or is there a better way to make sure that the user has python 2.7.x
in their environment variables?
This regular expression matches all non-alphanumeric characters: /[^a-zA-Z0-9]/
.
If you want to match all non-alphanumeric characters excluding forward-slash and backslash, use /[^a-zA-Z0-9\/\\]/
.
Examples:
str = 'C:\Python27\;C:\Python27\Scripts;C:\ProgramData\Oracle\Java\javapath'
str.split /[^a-zA-Z0-9\/\\]/
# => ["C", "\\Python27\\", "C", "\\Python27\\Scripts", "C", "\\ProgramData\\Oracle\\Java\\javapath"]
str = '/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games'
str.split /[^a-zA-Z0-9\/\\]/
# => ["/usr/local/bin", "/usr/bin", "/bin", "/usr/local/games", "/usr/games"]