Skip to content

Instantly share code, notes, and snippets.

@clicube
clicube / spec_helper.rb
Last active December 14, 2015 02:59
To use SimpleCov with fork, add this code to spec_helper.rb
pid = Process.pid
SimpleCov.at_exit do
SimpleCov.result.format! if Process.pid == pid
end
module MyModule
def pid
"MyModule.pid"
end
end
module ExtendModule
def pid
"ExtendModule.pid"
end
class Thread
def self.try_handle_interrupt *args
if respond_to?(:handle_interrupt)
self.handle_interrupt(*args){ yield }
else
yield
end
end
end
LIMIT = {}
def limit n
key = [caller,Thread.current]
LIMIT[key] ||= n
LIMIT[key] -= 1
n = LIMIT[key]
if n > 0
n
else
LIMIT.delete key
#include <sys/types.h>
#include <sys/wait.h>
#include <ruby.h>
/* include/ruby/ruby.h */
#ifndef IDTYPET2NUM
#define IDTYPET2NUM(v) INT2FIX(v)
#endif
#ifndef NUM2IDTYPET
#define NUM2IDTYPET(v) FIX2INT(v)
@clicube
clicube / process.rb
Last active December 14, 2015 20:18
Process::Process class
module Process
class ProcessError < StandardError; end
class Process
attr_reader :pid
def initialize(*args, &block)
raise ArgumentError.new("block must be given") unless block_given?
class Module
def wrap_package_private name
wraped_method = instance_method(name)
define_method(name) do |*args|
# check
puts "checking package_private: #{name}"
if true
e = NoMethodError.new("package private method is called from outside of the package")
e.set_backtrace caller
@clicube
clicube / quine.rb
Last active December 18, 2015 12:18
my first quine
s=%(s=%(X);s.sub!('X',s);puts s);s.sub!('X',s);puts s
require 'socket'
require 'thread'
query = <<QUERY
M-SEARCH * HTTP/1.1
HOST: 239.255.255.250:1900
MAN: "ssdp:discover"
MX: 3
ST: urn:schemas-upnp-org:device:InternetGatewayDevice:1
QUERY
@clicube
clicube / compress.rb
Last active December 19, 2015 22:59
binary file to ruby script file
# usage: ruby compress.rb icon.gif > myicon.rb
bit = 15
data = File.open(ARGV[0]){|f| f.read }
encoded_data = nil
offset = 0
bit_str = data.unpack("B*")[0]
(0x0000..0xffff).each do |i|