Skip to content

Instantly share code, notes, and snippets.

@lian
lian / gist:7345710
Created November 6, 2013 22:55
locale.rb
require 'ffi'
class Time
module Strftime
extend FFI::Library
ffi_lib 'c'
attach_function :setlocale, [:int, :string], :uint
attach_function :strftime, [:string, :long, :string, :pointer], :long
setlocale(LC_ALL = 6, "de_DE")
end
@lian
lian / gist:7345679
Created November 6, 2013 22:53
ffi-fiddle-wrapper.rb
# http://www.slideshare.net/tenderlove/hidden-gems-of-ruby-19 on slide 134
module FFI
module Library
TYPE_MAP = {
string: DL::TYPE_VOIDP,
pointer: DL::TYPE_VOIDP,
}
DL.constants.each{|const|
next unless const.to_s.match(/^TYPE_/)
class Mesh
# ...
def draw
@meshes.each.with_index{|(vao, gl_type, faces_count),mesh_index|
GL.glBindTexture(GL::GL_TEXTURE_2D, @tex_ids[mesh_index]) if @tex_ids[mesh_index]
GL.glBindVertexArray(vao)
GL.glDrawElements(gl_type, faces_count, GL::GL_UNSIGNED_SHORT, 0)
}
end
# ...
@lian
lian / gist:6705248
Last active December 23, 2015 22:40
is there a nicer way for Face#faces_from_one_ptr and Face#faces_from_multiple_ptrs
=begin
# C dummy code
struct face
{
unsigned int mNumIndices;
unsigned int* mIndices;
}
struct mesh
{
require 'ffi'
# simple GeoLiteCity IP lookup
#
# install:
# * archlinux => pacman -S geoip
# * database at http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
#
module GeoIP
@lian
lian / gist:4018336
Created November 5, 2012 17:03
pronterface.sh on archlinux
#!/bin/bash
cd "$(dirname $(readlink -f $0))"
python2 pronterface.py -c config/slic3r $@
@lian
lian / slic3r bundled install on archlinux
Created November 5, 2012 17:02
slic3r bundled install on archlinux
#!/bin/bash
# install on archlinux:
# % pacman -S cpanminus
# % cd /path/to/Slic3r-git
# % HOME="`pwd`/vendor" cpanm -n --local-lib="`pwd`/vendor" Boost::Geometry::Utils Math::Clipper Math::ConvexHull Math::ConvexHull::MonotoneChain Math::Geometry::Voronoi Math::PlanePath Moo Wx
cd "$(dirname $(readlink -f $0))"
PERL5LIB="vendor/lib/perl5:vendor/lib/perl5/x86_64-linux-thread-multi" ./slic3r.pl $@
require 'bundler/setup'
require 'reel'
require 'rack'
class RequestActor
include Celluloid
def initialize(app, connection); @app, @connection = app, connection; end
def run
env = @connection.request.headers
class Hash
alias get []
def [](*a)
a.size == 1 ? get(*a) : (self[a.shift][*a] rescue nil)
end
end
p( {}[:a, :b] )
p( {:a => {:b => "c"}}[:a, :b] )
p( {:a => {:b => "c"}}[:a, :b, :c] )
require 'ffi'
class FFI::Struct
def method_missing(m=nil, *a)
m ? self[m] : super(m, *a)
end
def self.array(size)
klass = self
Class.new(FFI::Struct){ layout :array, [klass, size]; def [](idx); super(:array)[idx]; end; }.new
end