Skip to content

Instantly share code, notes, and snippets.

View brainopia's full-sized avatar

Ravil Bayramgalin brainopia

View GitHub Profile
require 'fiddle'
A = Class.new
B = Class.new
C = Class.new A
p C.superclass # => A
(Fiddle::Pointer[Fiddle.dlwrap C] + 16)[0, 8] = Fiddle::Pointer[Fiddle.dlwrap B].ref
p C.superclass # => B
@brainopia
brainopia / gist:3910381
Created October 18, 2012 08:06
Github mirror
require 'eventmachine'
class Request < EM::P::HeaderAndContentProtocol
CRLF = "\r\n"
def receive_request(headers, body)
receive_full_request headers.join(CRLF) << CRLF*2 << body
end
end
@brainopia
brainopia / gist:3910380
Created October 18, 2012 08:06
Github proxy
require 'em-proxy'
Proxy.start host: '0.0.0.0', port: ENV['PORT'] do |connection|
connection.server :github, host: 'api.github.com', port: 443
ok_response = "HTTP/1.1 200 Ok\r\n\r\n"
connect_verb = /\Aconnect/i
connection.on_data do |data|
if data =~ connect_verb
@brainopia
brainopia / openssl.sh
Created October 2, 2012 06:55
Fix readline/openssl extension
cd $rvm_path/src/${MY_RUBY_HOME##*/}/ext/openssl
rvm pkg install openssl
ruby extconf.rb -- --with-openssl-dir="$rvm_path/usr"
make install
cd $rvm_path/usr/ssl
curl http://curl.haxx.se/ca/cacert.pem > cert.pem
module A
# prefer when there are less than three class methods
# and there are instance methods
def self.boo
end
def self.soo
end
def fuuu
# eval with method definition is used to prevent warnings in some situations
DEFINEE = <<-CODE
eval 'def __; end'
it = method(:__).owner rescue instance_method(:__).owner
eval 'undef __'
it
CODE
self # => main
# Usually normalization is performed during `before_validation`
# or in a setter. But neither would help with normalization of
# where conditions.
#
# This will add `normalize` to ActiveRecord with support for
# where conditions. For example,
#
# class User
# normalize(:email) { email.downcase }
# end
(function() {
if (window.setImmediate) return;
var timeouts = [];
var messageName = "zero-timeout-message";
// Like setTimeout, but only takes a function argument. There's
// no time argument (always zero) and no arguments (you have to
// use a closure).
function setZeroTimeout(fn) {
class Chart
@initAll: ->
Bar.init()
Spark.init()
@init: ->
for element in $(@selector)
do (element) =>
setImmediate =>
unless $(element).data('initialized')
@brainopia
brainopia / gist:2215241
Created March 27, 2012 11:47
Set bom recursively to ruby files
#!/usr/bin/env ruby
require 'epath'
bom = "\xEF\xBB\xBF".force_encoding('binary')
Path.glob('**/*.rb') do |file|
next if file.binread(3) == bom
previous = file.binread
file.write bom + previous
end