R(u)b(y)Config(uration)

At some point when working with Ruby, you come across this mysterious RbConfig constant. A typical scenario is that you want to check which operating system your current program is executed on. You can do this with RbConfig::CONFIG['host_os'] or RbConfig::CONFIG['arch'], see the RubyGems source for an advanced example!

The Ruby Configuration is a collection of low-level information about your operating system, which gets created by mkconfig.rb when Ruby is compiled. It mostly contains compiler flags generated by Ruby's compile scripts. Its exact contents depend on your system, and also on your Ruby implementation and version. All major Ruby implementations have a RbConfig.

The majority of its data can be found in a hash called RbConfig::CONFIG and you will need to require "rbconfig" before using it. There is additional data in other constants, such as in RbConfig::SIZEOF, which was added with Ruby 2.3. What follows is an overview of what can be found in RbConfig:

RbConfig.ruby

Returns the absolute pathname of the ruby command, constructed from bindir, ruby_install_name, and EXEEXT (see table below):

require "rbconfig"
RbConfig.ruby #=> "/home/jan/.rvm/rubies/ruby-2.3.0/bin/ruby"

RbConfig.expand(val)

The RbConfig object has an internal method to substitute placeholders in values with other values from RbConfig. For example, the LIBRUBY_SO value could be converted from "lib$(RUBY_SO_NAME).so.$(MAJOR).$(MINOR).$(TEENY)" to "libruby.so.2.3.0".

RbConfig::TOPDIR

Returns the directory where the Ruby interpreter is located:

require "rbconfig"
RbConfig::TOPDIR #=> "/home/jan/.rvm/rubies/ruby-2.3.0"

RbConfig::DESTDIR

If you set the ::DESTDIR constant before loading "rbconfig", it will have an impact on some other values (see the table below).

RbConfig::MAKEFILE_CONFIG

A copy of RbConfig::CONFIG without expanded values (see table in next section).

RbConfig::CONFIG

The following table contains the example output on an ubuntu machine using Ruby 2.3.0, installed via RVM:

Config Key Value Construction (Non-Expanded) Final Value (Expanded)
DESTDIR DESTDIR ""
MAJOR "2" "2"
MINOR "3" "3"
TEENY "0" "0"
PATCHLEVEL "0" "0"
INSTALL '/usr/bin/install -c' "/usr/bin/install -c"
EXEEXT "" ""
prefix (TOPDIR || DESTDIR + "/home/jan/.rvm/rubies/ruby-2.3.0") "/home/jan/.rvm/rubies/ruby-2.3.0"
ruby_install_name "$(RUBY_BASE_NAME)" "ruby"
RUBY_INSTALL_NAME "$(RUBY_BASE_NAME)" "ruby"
RUBY_SO_NAME "$(RUBY_BASE_NAME)" "ruby"
exec "exec" "exec"
ruby_pc "ruby-2.3.pc" "ruby-2.3.pc"
PACKAGE "ruby" "ruby"
BUILTIN_TRANSSRCS " enc/trans/newline.c" " enc/trans/newline.c"
USE_RUBYGEMS "YES" "YES"
MANTYPE "doc" "doc"
NROFF "/usr/bin/nroff" "/usr/bin/nroff"
vendorarchhdrdir "$(vendorhdrdir)/$(sitearch)" "/home/jan/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0/vendor_ruby/x86_64-linux"
sitearchhdrdir "$(sitehdrdir)/$(sitearch)" "/home/jan/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0/site_ruby/x86_64-linux"
rubyarchhdrdir "$(rubyhdrdir)/$(arch)" "/home/jan/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0/x86_64-linux"
vendorhdrdir "$(rubyhdrdir)/vendor_ruby" "/home/jan/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0/vendor_ruby"
sitehdrdir "$(rubyhdrdir)/site_ruby" "/home/jan/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0/site_ruby"
rubyhdrdir "$(includedir)/$(RUBY_VERSION_NAME)" "/home/jan/.rvm/rubies/ruby-2.3.0/include/ruby-2.3.0"
RUBY_SEARCH_PATH "" ""
UNIVERSAL_INTS "" ""
UNIVERSAL_ARCHNAMES "" ""
configure_args " '--prefix=/home/jan/.rvm/rubies/ruby-2.3.0' '--disable-install-doc' '--enable-shared'" " '--prefix=/home/jan/.rvm/rubies/ruby-2.3.0' '--disable-install-doc' '--enable-shared'"
CONFIGURE "configure" "configure"
vendorarchdir "$(vendorlibdir)/$(sitearch)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0/x86_64-linux"
vendorlibdir "$(vendordir)/$(ruby_version)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby/2.3.0"
vendordir "$(rubylibprefix)/vendor_ruby" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/vendor_ruby"
sitearchdir "$(sitelibdir)/$(sitearch)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/x86_64-linux"
sitelibdir "$(sitedir)/$(ruby_version)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0"
sitedir "$(rubylibprefix)/site_ruby" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby"
rubyarchdir "$(rubylibdir)/$(arch)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-linux"
rubylibdir "$(rubylibprefix)/$(ruby_version)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0"
ruby_version "2.3.0" "2.3.0"
sitearch "$(arch)" "x86_64-linux"
arch "x86_64-linux" "x86_64-linux"
sitearchincludedir "$(includedir)/$(sitearch)" "/home/jan/.rvm/rubies/ruby-2.3.0/include/x86_64-linux"
archincludedir "$(includedir)/$(arch)" "/home/jan/.rvm/rubies/ruby-2.3.0/include/x86_64-linux"
sitearchlibdir "$(libdir)/$(sitearch)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/x86_64-linux"
archlibdir "$(libdir)/$(arch)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/x86_64-linux"
libdirname "libdir" "libdir"
RUBY_EXEC_PREFIX "/home/jan/.rvm/rubies/ruby-2.3.0" "/home/jan/.rvm/rubies/ruby-2.3.0"
RUBY_LIB_VERSION "" ""
RUBY_LIB_VERSION_STYLE "3\t/* full */" "3\t/* full */"
RI_BASE_NAME "ri" "ri"
ridir "$(datarootdir)/$(RI_BASE_NAME)" "/home/jan/.rvm/rubies/ruby-2.3.0/share/ri"
rubysitearchprefix "$(rubylibprefix)/$(sitearch)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/x86_64-linux"
rubyarchprefix "$(rubylibprefix)/$(arch)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/x86_64-linux"
MAKEFILES "Makefile GNUmakefile" "Makefile GNUmakefile"
PLATFORM_DIR "" ""
THREAD_MODEL "pthread" "pthread"
SYMBOL_PREFIX "" ""
EXPORT_PREFIX "" ""
COMMON_HEADERS "" ""
COMMON_MACROS "" ""
COMMON_LIBS "" ""
MAINLIBS "" ""
ENABLE_SHARED "yes" "yes"
DLDLIBS " -lc" " -lc"
SOLIBS "$(LIBS)" "-lpthread -lgmp -ldl -lcrypt -lm "
LIBRUBYARG_SHARED "-Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)" "-Wl,-R/home/dan/.rvm/rubies/ruby-2.3.0/lib -L/home/dan/.rvm/rubies/ruby-2.3.0/lib -lruby"
LIBRUBYARG_STATIC "-Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static" "-Wl,-R/home/dan/.rvm/rubies/ruby-2.3.0/lib -L/home/dan/.rvm/rubies/ruby-2.3.0/lib -lruby-static"
LIBRUBYARG "$(LIBRUBYARG_SHARED)" "-Wl,-R/home/dan/.rvm/rubies/ruby-2.3.0/lib -L/home/dan/.rvm/rubies/ruby-2.3.0/lib -lruby"
LIBRUBY "$(LIBRUBY_SO)" "libruby.so.2.3.0"
LIBRUBY_ALIASES "lib$(RUBY_SO_NAME).so.$(MAJOR).$(MINOR) lib$(RUBY_SO_NAME).so" "libruby.so.2.3 libruby.so"
LIBRUBY_SO "lib$(RUBY_SO_NAME).so.$(MAJOR).$(MINOR).$(TEENY)" "libruby.so.2.3.0"
LIBRUBY_A "lib$(RUBY_SO_NAME)-static.a" "libruby-static.a"
RUBYW_INSTALL_NAME "" ""
rubyw_install_name "" ""
EXTDLDFLAGS "" ""
EXTLDFLAGS "" ""
strict_warnflags "-std=iso9899:1999" "-std=iso9899:1999"
warnflags "-Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wno-maybe-uninitialized" "-Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wno-maybe-uninitialized"
debugflags "-ggdb3" "-ggdb3"
optflags "-O3 -fno-fast-math" "-O3 -fno-fast-math"
NULLCMD ":" ":"
DLNOBJ "dln.o" "dln.o"
EXECUTABLE_EXTS "" ""
ARCHFILE "" ""
LIBRUBY_RELATIVE "no" "no"
EXTOUT ".ext" ".ext"
PREP "miniruby$(EXEEXT)" "miniruby"
CROSS_COMPILING "no" "no"
TEST_RUNNABLE "yes" "yes"
rubylibprefix "$(libdir)/$(RUBY_BASE_NAME)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby"
setup "Setup" "Setup"
ENCSTATIC "" ""
EXTSTATIC "" ""
STRIP "strip -S -x" "strip -S -x"
TRY_LINK "" ""
PRELOADENV "LD_PRELOAD" "LD_PRELOAD"
LIBPATHENV "LD_LIBRARY_PATH" "LD_LIBRARY_PATH"
RPATHFLAG " -Wl,-R%1$-s" " -Wl,-R%1$-s"
LIBPATHFLAG " -L%1$-s" " -L%1$-s"
LINK_SO "" ""
ASMEXT "S" "S"
LIBEXT "a" "a"
DLEXT2 "" ""
DLEXT "so" "so"
LDSHAREDXX "$(CXX) -shared" "g++ -shared"
LDSHARED "$(CC) -shared" "gcc -shared"
CCDLFLAGS "-fPIC" "-fPIC"
STATIC "" ""
ARCH_FLAG "" ""
DLDFLAGS "" ""
ALLOCA "" ""
codesign "" ""
POSTLINK ":" ":"
WERRORFLAG "-Werror" "-Werror"
CHDIR "cd -P" "cd -P"
RMALL "rm -fr" "rm -fr"
RMDIRS "rmdir --ignore-fail-on-non-empty -p" "rmdir --ignore-fail-on-non-empty -p"
RMDIR "rmdir --ignore-fail-on-non-empty" "rmdir --ignore-fail-on-non-empty"
CP "cp" "cp"
RM "rm -f" "rm -f"
PKG_CONFIG "pkg-config" "pkg-config"
PYTHON "" ""
DOXYGEN "" ""
DOT "dot" "dot"
MAKEDIRS "/bin/mkdir -p" "/bin/mkdir -p"
MKDIR_P "/bin/mkdir -p" "/bin/mkdir -p"
INSTALL_DATA "$(INSTALL) -m 644" "/usr/bin/install -c -m 644"
INSTALL_SCRIPT "$(INSTALL)" "/usr/bin/install -c"
INSTALL_PROGRAM "$(INSTALL)" "/usr/bin/install -c"
SET_MAKE "" ""
LN_S "ln -s" "ln -s"
NM "nm" "nm"
DLLWRAP "" ""
WINDRES "" ""
OBJCOPY ":" ":"
OBJDUMP "objdump" "objdump"
ASFLAGS "" ""
AS "as" "as"
AR "ar" "ar"
RANLIB "ranlib" "ranlib"
try_header "" ""
CC_VERSION "$(CC) -v" "gcc -v"
COUTFLAG "-o " "-o "
OUTFLAG "-o " "-o "
CPPOUTFILE "-o conftest.i" "-o conftest.i"
GNU_LD "yes" "yes"
LD "ld" "ld"
GCC "yes" "yes"
EGREP "/bin/grep -E" "/bin/grep -E"
GREP "/bin/grep" "/bin/grep"
CPP "$(CC) -E" "gcc -E"
CXXFLAGS "$(cxxflags)" " -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wno-maybe-uninitialized"
OBJEXT "o" "o"
CPPFLAGS " $(DEFS) $(cppflags)" " "
LDFLAGS "-L. -fstack-protector -rdynamic -Wl,-export-dynamic" "-L. -fstack-protector -rdynamic -Wl,-export-dynamic"
CFLAGS "$(cflags) -fPIC" " -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wno-maybe-uninitialized -fPIC"
CXX "g++" "g++"
CC "gcc" "gcc"
NACL_LIB_PATH "" ""
NACL_SDK_VARIANT "" ""
NACL_SDK_ROOT "" ""
NACL_TOOLCHAIN "" ""
target_os "linux" "linux"
target_vendor "pc" "pc"
target_cpu "x86_64" "x86_64"
target "x86_64-pc-linux-gnu" "x86_64-pc-linux-gnu"
host_os "linux-gnu" "linux-gnu"
host_vendor "pc" "pc"
host_cpu "x86_64" "x86_64"
host "x86_64-pc-linux-gnu" "x86_64-pc-linux-gnu"
RUBY_VERSION_NAME "$(RUBY_BASE_NAME)-$(ruby_version)" "ruby-2.3.0"
RUBYW_BASE_NAME "rubyw" "rubyw"
RUBY_BASE_NAME "ruby" "ruby"
build_os "linux-gnu" "linux-gnu"
build_vendor "pc" "pc"
build_cpu "x86_64" "x86_64"
build "x86_64-pc-linux-gnu" "x86_64-pc-linux-gnu"
RUBY_PROGRAM_VERSION "2.3.0" "2.3.0"
cxxflags " $(optflags) $(debugflags) $(warnflags)" " -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wno-maybe-uninitialized"
cppflags "" ""
cflags " $(optflags) $(debugflags) $(warnflags)" " -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wno-maybe-uninitialized"
target_alias "" ""
host_alias "" ""
build_alias "" ""
LIBS "-lpthread -lgmp -ldl -lcrypt -lm " "-lpthread -lgmp -ldl -lcrypt -lm "
ECHO_T "" ""
ECHO_N "-n" "-n"
ECHO_C "" ""
DEFS "" ""
mandir "$(datarootdir)/man" "/home/jan/.rvm/rubies/ruby-2.3.0/share/man"
localedir "$(datarootdir)/locale" "/home/jan/.rvm/rubies/ruby-2.3.0/share/locale"
libdir "$(exec_prefix)/lib" "/home/jan/.rvm/rubies/ruby-2.3.0/lib"
psdir "$(docdir)" "/home/jan/.rvm/rubies/ruby-2.3.0/share/doc/ruby"
pdfdir "$(docdir)" "/home/jan/.rvm/rubies/ruby-2.3.0/share/doc/ruby"
dvidir "$(docdir)" "/home/jan/.rvm/rubies/ruby-2.3.0/share/doc/ruby"
htmldir "$(docdir)" "/home/jan/.rvm/rubies/ruby-2.3.0/share/doc/ruby"
infodir "$(datarootdir)/info" "/home/jan/.rvm/rubies/ruby-2.3.0/share/info"
docdir "$(datarootdir)/doc/$(PACKAGE)" "/home/jan/.rvm/rubies/ruby-2.3.0/share/doc/ruby"
oldincludedir "/usr/include" "/usr/include"
includedir "$(prefix)/include" "/home/jan/.rvm/rubies/ruby-2.3.0/include"
localstatedir "$(prefix)/var" "/home/jan/.rvm/rubies/ruby-2.3.0/var"
sharedstatedir "$(prefix)/com" "/home/jan/.rvm/rubies/ruby-2.3.0/com"
sysconfdir "$(prefix)/etc" "/home/jan/.rvm/rubies/ruby-2.3.0/etc"
datadir "$(datarootdir)" "/home/jan/.rvm/rubies/ruby-2.3.0/share"
datarootdir "$(prefix)/share" "/home/jan/.rvm/rubies/ruby-2.3.0/share"
libexecdir "$(exec_prefix)/libexec" "/home/jan/.rvm/rubies/ruby-2.3.0/libexec"
sbindir "$(exec_prefix)/sbin" "/home/jan/.rvm/rubies/ruby-2.3.0/sbin"
bindir "$(exec_prefix)/bin" "/home/jan/.rvm/rubies/ruby-2.3.0/bin"
exec_prefix "$(prefix)" "/home/jan/.rvm/rubies/ruby-2.3.0"
PACKAGE_URL "" ""
PACKAGE_BUGREPORT "" ""
PACKAGE_STRING "" ""
PACKAGE_VERSION "" ""
PACKAGE_TARNAME "" ""
PACKAGE_NAME "" ""
PATH_SEPARATOR ":" ":"
SHELL "/bin/bash" "/bin/bash"
archdir "$(rubyarchdir)" "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-linux"
topdir File.dirname(__FILE__) "/home/jan/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/x86_64-linux"

RbConfig::SIZEOF

This hash contains the byte sizes of low-level data structures of your system. This is useful when interacting with external libraries via FFI/Fiddle. I needs to be explicitly required:

require "rbconfig/sizeof"
RbConfig::SIZEOF # => {"int"=>4, "short"=>2, "long"=>8, "long long"=>8, ...

The following table contains example values from an ubuntu machine using Ruby 2.3.0:

Type Size
int 4
short 2
long 8
long long 8
off_t 8
void* 8
float 4
double 8
time_t 8
clock_t 8
size_t 8
ptrdiff_t 8
int8_t 1
uint8_t 1
int16_t 2
uint16_t 2
int32_t 4
uint32_t 4
int64_t 8
uint64_t 8
intptr_t 8
uintptr_t 8
ssize_t 8
int_least8_t 1
int_least16_t 2
int_least32_t 4
int_least64_t 8
int_fast8_t 1
int_fast16_t 8
int_fast32_t 8
int_fast64_t 8
intmax_t 8
sig_atomic_t 4
wchar_t 4
wint_t 4
wctrans_t 8
wctype_t 8
_Bool 1
long double 16
float _Complex 8
double _Complex 16
long double _Complex 32
__int128 16
__float128 16
_Decimal32 4
_Decimal64 8
_Decimal128 16
__float80 16

More Idiosyncratic Ruby