Using sysconf in Ruby with FFI (Update)


Turns out I was wrong yesterday and what I got was garbage.

So let's do it right today, but unfortunately this is going to be a lot less pretty than I'd hoped for. Also, this is going to use a C compiler in the backround every time you run it. Might be worth caching.

require 'ffi'
require 'ffi/tools/const_generator'

module Sysconf
  extend FFI::Library
  ffi_lib ["c"]

  fcg = do |gen|
    gen.include 'unistd.h'
    ].each do |const|
      ruby_name = const.sub(/^_SC_/, '').downcase.to_sym
      gen.const(const, "%d", nil, ruby_name, &:to_i)

  CONF = enum(*{|_, const|
    [const.ruby_name, const.converted_value]

  attach_function :sysconf, [CONF], :long

p page_size: Sysconf.sysconf(:page_size)
p version: Sysconf.sysconf(:version)