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 IO | |
TIOCGWINSZ = 0x5413 | |
TIOCSWINSZ = 0x5414 | |
Winsize = Struct.new(:rows, :columns, :horizontal_pixels, :vertical_pixels) | |
def winsize | |
ioctl(TIOCGWINSZ, size=""); Winsize.new(*size.unpack("SSSS")) | |
end | |
def winsize=(winsize) |
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
module B | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
def foo | |
p 'from instance' | |
end | |
module ClassMethods |
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 Options < Hash | |
def self.new(&a) | |
a = lambda{|h,k| h[k] = new(&a) } unless a | |
super(&a) | |
end | |
def method_missing(m, *a) | |
if m[-1] == "=" | |
key = keys.find{|k| k.to_s == m[0...-1].to_s } || m[0...-1].to_sym | |
self[key] = a.first | |
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
h = Hash.new{|h,k| h[k] = 0 }; ObjectSpace.each_object{|o| h[o.class] += 1 }; p h.sort_by{|k,v| -v} |
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 'socket' | |
serv = TCPServer.new("127.0.0.1", 110) | |
s = serv.accept | |
s.puts "+OK POP3 server ready" | |
while line = s.gets.strip | |
case line | |
when "CAPA" | |
s.puts("SASL PLAIN\n.") |
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 'securerandom' | |
N = 100_000 | |
A = %w(a b c) | |
Score = Hash.new { |h, k| h[k] = 0 } | |
N.times do | |
sorted = A.sort_by{ SecureRandom.random_bytes(4).unpack("I")[0] } | |
Score[sorted.join(" ")] += 1 | |
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 'eventmachine' | |
children = [] | |
Signal.trap('INT') do | |
puts "Killing #{children.size} children ..." | |
children.each do |pid| | |
Process.kill('SIGUSR1', pid) rescue nil | |
end | |
EM.stop |
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 ResistorCalculator | |
# http://www.dannyg.com/examples/res2/resistor.htm | |
attr_reader :tolerance, :multiplier, :ones, :tens, :ohms | |
Tens = %w[Black Brown Red Orange Yellow Green Blue Violet Gray White] | |
Ones = %w[Black Brown Red Orange Yellow Green Blue Violet Gray White] | |
Multiplier = %w[Black Brown Red Orange Yellow Green Blue Violet Gray White Gold Silver] | |
Tolerance = %w[Gold Silver None] |
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 'open-uri'; require 'json' | |
username = ARGV[0] | |
JSON.parse(open("http://gist.github.com/api/v1/json/gists/#{username}").read)['gists'].each{|i| system "git clone git://gist.github.com/#{i['repo']} gist-#{i['repo']}" } |
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
module Base32 | |
TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567".downcase | |
class Chunk < Struct.new(:bytes) | |
def decode | |
b = bytes.take_while{|c| c != 61 } # strip padding | |
n = (b.length * 5.0 / 8.0).floor | |
p = b.length < 8 ? 5 - (n * 8) % 5 : 0 | |
c = b.inject(0){|m,o| (m << 5) + TABLE.index(o.chr) } >> p | |
(0...n).to_a.reverse.map{|i| ((c >> i * 8) & 0xff).chr } |
NewerOlder