Skip to content

Instantly share code, notes, and snippets.

@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
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 / 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?
#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)
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
class Thread
def self.try_handle_interrupt *args
if respond_to?(:handle_interrupt)
self.handle_interrupt(*args){ yield }
else
yield
end
end
end
module MyModule
def pid
"MyModule.pid"
end
end
module ExtendModule
def pid
"ExtendModule.pid"
end
@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
@clicube
clicube / ipv4header.rb
Created February 18, 2013 17:02
Read TCP, UDP, IP header by Ruby
# coding: utf-8
require_relative 'protocol'
# TODO: チェックサムを確認する
class IPv4Header
attr_reader :version,:header_length,:packet_length,:identification,:frag_dont,:frag_more,:frag_offset,:ttl,:protocol,:checksum,:sndr_addr,:dest_addr,
:data_length
def initialize(packet,offset=0)
@clicube
clicube / humanlike_mechanize.rb
Created February 8, 2013 17:13
HumanlikeMechanize waits before each get/post
require 'mechanize'
class HumanlikeMechanize < Mechanize
def initialize min,max
raise ArgumentError.new("min > max") if min > max
raise ArgumentError.new("min < 0") if min < 0
raise ArgumentError.new("max < 0") if max < 0
@sleep_min = min
@sleep_max = max