In Ruby, I would use the RUBY_PLATFORM constant to determine what operating system (Mac, Windows, Linux etc) my program is running on. Does Elixir have a way to get this information?
I'm currently attempting to re-create a Ruby program I wrote in Elixir, and I have a method that will make an OS-dependent system call to open a document. The method looks something like:
def self.open_document(filename)
case RUBY_PLATFORM
when %r(darwin)
system('open', filename)
when %r(linux)
system('xdg-open', filename)
when %r(windows)
system('cmd', '/c', "\"start #{filename}\"")
else
puts "Don't know how to open file"
end
end
I know I can run the Ruby Kernel.system commands using Elixir's System.cmd/3, but I'm not sure how to get the RUBY_PLATFORM value equivalent to make the switch on in the case statement, or whether I can actually get that information. Is this possible?
As per Lol4t0's answer and for further reference:
iex> :os.type
{:unix, :darwin}
iex> System.cmd("uname", ["-s"])
{"Darwin\n", 0}
You can call Erlang os:type to get platform name info:
type() -> {Osfamily, Osname}Types:
Osfamily = unix | win32 Osname = atom()Returns the Osfamily and, in some cases, Osname of the current operating system.
On Unix, Osname will have same value as
uname -sreturns, but in lower case. For example, on Solaris 1 and 2, it will be sunos.In Windows, Osname will be either
nt(on Windows NT), orwindows(on Windows 95).
In Elixir you probably have to call
:os.type()
to refer to that function with Osfamily being :unix or :win32.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With