This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'ffi' | |
class Foo | |
extend FFI::Library | |
ffi_lib('c') | |
attach_function :sysctlbyname, | |
[:string, :pointer, :pointer, :pointer, :size_t], | |
:int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
class << self | |
def bar; end | |
# Result is 'NOPE' - why? | |
if respond_to?(:bar) | |
puts "YEP" | |
else | |
puts "NOPE" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
Alpha = 1 | |
Beta = 2 | |
Gamma = 4 | |
@@opts = { | |
Alpha => 'alpha', | |
Beta => 'beta', | |
Gamma => 'gamma' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
def mktemp(template) | |
regex = /^([^X].*?)(X)(XXXXX)/ | |
unless m = regex.match(template) | |
raise "Invalid template: #{template}" | |
end | |
chars = ('a'..'z').to_a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |