Quite often Ruby installed with RVM (the ‘most popular tool to install Ruby’) produces a binary not capable of SSL operations (like connecting via HTTPS). It’s a known issue with a known workaround.
If you’re trying to use SSL with such Ruby binary it results in exception LoadError: cannot load such file -- openssl.
Question: is there any way I can tell which libs Ruby binary was built against and thus detect if it is capable of SSL or not at runtime? Other than trying to actually use SSL and catching the exception?
Unless you want to do the same work that require does, I think the easiest is to try to require openssl and rescue the LoadError in case it’s not present on the system to detect its availability.
It seems to be an idiom commonly used.
If you wanted something that detects afterwards if it was loaded or not would
vs.
help you?
That is, checking whether the return value of
defined?(OpenSSL)is non-nil?We commonly use this in tests to exclude tests at runtime where features that are not available to all versions of OpenSSL are used, and to exclude tests completely (
if defined? OpenSSL) if OpenSSL is not present as an extension.