Skip to content

Instantly share code, notes, and snippets.

View djberg96's full-sized avatar

Daniel Berger djberg96

View GitHub Profile
@djberg96
djberg96 / ffi_test.rb
Created December 13, 2011 20:50
Trying to get sysctlbyname working with ffi
require 'ffi'
class Foo
extend FFI::Library
ffi_lib('c')
attach_function :sysctlbyname,
[:string, :pointer, :pointer, :pointer, :size_t],
:int
@djberg96
djberg96 / proto.rb
Created December 17, 2011 14:44
custom read_array_of_string method dies
class FFI::Pointer
# This works fine on MRI, but dies on JRuby
def read_array_of_string
elements = []
psz = RUBY_PLATFORM == 'java' ? 4 : self.class.size
loc = self
until ((element = loc.read_pointer).null?)
elements << element.read_string
@djberg96
djberg96 / uri_build_profile.txt
Created December 23, 2011 16:25
Ruby URI.build profile
# 10,000 iterations of URI.build
Thread ID: 2148426160
Total: 3.451971
%self total self wait child calls name
13.51 1.96 0.47 0.00 1.49 10000 URI::Generic#initialize (/usr/local/lib/ruby/1.8/uri/generic.rb:161}
12.44 0.56 0.43 0.00 0.13 10000 Array#collect (ruby_runtime:0}
4.91 0.58 0.17 0.00 0.41 10000 <Module::URI::Util>#make_components_hash (/usr/local/lib/ruby/1.8/uri/common.rb:218}
3.97 2.77 0.14 0.00 2.64 10000 <Class::URI::Generic>#build (/usr/local/lib/ruby/1.8/uri/generic.rb:108}
@djberg96
djberg96 / respond_to_test.rb
Created December 25, 2011 14:56
Confused about respond_to and singleton methods
class Foo
class << self
def bar; end
# Result is 'NOPE' - why?
if respond_to?(:bar)
puts "YEP"
else
puts "NOPE"
end
@djberg96
djberg96 / getmntinfo.rb
Created January 5, 2012 04:21
Trying to use getmntinfo with ffi
require 'ffi'
class Filesystem
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function(:getmntinfo, [:pointer, :int], :int)
attach_function(:strerror, [:int], :string)
class Statfs < FFI::Struct
@djberg96
djberg96 / bit_fiddler.rb
Created January 10, 2012 19:31
Converting constants to human readable strings
class Foo
Alpha = 1
Beta = 2
Gamma = 4
@@opts = {
Alpha => 'alpha',
Beta => 'beta',
Gamma => 'gamma'
}
@djberg96
djberg96 / flag_to_string.rb
Created January 10, 2012 20:02
Trying to stringify filesystem flags
require 'ffi'
class Filesystem
extend FFI::Library
ffi_lib FFI::Library::LIBC
attach_function(:strerror, [:int], :string)
attach_function(:getmntinfo64, [:pointer, :int], :int)
class Statfs < FFI::Struct
@djberg96
djberg96 / private_class_method_test.rb
Created January 11, 2012 16:20
Problem with private_class_method using JRuby
require 'ffi'
module Sys
class Filesystem
extend FFI::Library
ffi_lib FFI::Library::LIBC
if RbConfig::CONFIG['host_os'] =~ /darwin|osx|mach/i
attach_function(:getmntinfo, :getmntinfo64, [:pointer, :int], :int)
else
@djberg96
djberg96 / mktemp.rb
Created January 17, 2012 19:49
Custom mktemp for Ruby
class Foo
def mktemp(template)
regex = /^([^X].*?)(X)(XXXXX)/
unless m = regex.match(template)
raise "Invalid template: #{template}"
end
chars = ('a'..'z').to_a
@djberg96
djberg96 / test.rb
Created January 19, 2012 15:09
Trouble with Rubinius and test-unit gem
# Works fine with MRI and JRuby.
# Rubinius doesn't seem to pick up the gem, blows up with an error.
require 'rubygems'
gem 'test-unit'
require 'test/unit'
class TC_Foo < Test::Unit::TestCase
test "test-unit 2 works as expected" do
assert_true(1 == 1)