Skip to content

Instantly share code, notes, and snippets.

@bradediger
bradediger / gist:4137531
Created November 23, 2012 22:14
Redis cache hit ratio
#!/usr/bin/env ruby
hits_ = nil
misses_ = nil
loop do
`redis-cli info | grep keyspace` =~ /hits:(\d+).*misses:(\d+)/m
hits, misses = $1.to_i, $2.to_i
if hits_
@bradediger
bradediger / gist:4118335
Created November 20, 2012 14:41
The holy grail... for vim / zsh users
foreground-vim() {
fg %vim
}
zle -N foreground-vim
bindkey '^Z' foreground-vim
table = pdf.make_table([["foo", "bar"],
["baz\n"*18, "quux"]], :position => :center)
pdf.move_down((pdf.bounds.height - table.height) / 2.0)
table.draw
obj = %{<< /Rect [ 330.521 116.285 346.958 128.321 ] /F 4 /T (Check Box1) /Subtype
/Widget /P 3 0 R /MK 139 0 R /AP 140 0 R /H /P /V / /AS /Off /FT /Btn /Type
/Annot >>}
buf = PDF::Reader::Buffer.new(StringIO.new(obj))
parser = PDF::Reader::Parser.new(buf, nil)
parser.parse_token
# => {:Rect=>[330.521, 116.285, 346.958, 128.321], :F=>4, :T=>"Check Box1", :Subtype=>:Widget, :P=>#<PDF::Reader::Reference:0x000000012afd40 @id=3, @gen=0>, :MK=>#<PDF::Reader::Reference:0x000000012ad298 @id=139, @gen=0>, :AP=>#<PDF::Reader::Reference:0x000000012accd0 @id=140, @gen=0>, :H=>:P, :V=>:"", :AS=>:Off, :FT=>:Btn, :Type=>:Annot}
require 'rubygems'
require 'prawn'
require 'barby'
require 'barby/barcode/qr_code'
require 'barby/outputter/prawn_outputter'
Prawn::Document.generate("barcode.pdf") do |pdf|
pdf.bounding_box([0, pdf.cursor], :width => 100, :height => 100) do
Barby::QrCode.new("hello", :level => :h).annotate_pdf(pdf)
end
require 'prawn'
class ChangingMarginDocument < Prawn::Document
def start_new_page(options={})
if (page_count % 2).zero?
super(:left_margin => 72, :right_margin => 36)
else
super(:left_margin => 36, :right_margin => 72)
end
end
Prawn::Document.generate("test_height.pdf") do |pdf|
box = Prawn::Text::Box.new("foo bar baz " * 50, :document => pdf,
:overflow => :shrink_to_fit, :height => 20)
box.render
puts box.height # >> 19.422
end
Prawn::Document.new do |pdf|
box = Prawn::Text::Box.new("foo bar baz " * 50, :document => pdf,
:overflow => :expand, :height => 1)
box.render
puts box.height # >> 80.46
end
Prawn::Document.generate 'test_table.pdf', :page_layout => :landscape do |pdf|
data = [(1..10).map { "foo bar baz " * 20 }] * 5
pdf.table data, :column_widths => [96,120,54,54,54,54,54,54,54,54] do |t|
t.column(0).style :overflow => :shrink_to_fit, :min_font_size => 6
end
end
# Sort lexicographically, but sorting numbers into their proper position.
# Ruby 1.9 only as the zero-width lookbehind assertions require Oniguruma.
#
class Array
def sort_preserving_numbers
sort_by { |x|
x.split(%r{(?<=\D)(?=\d)|(?<=\d)(?=\D)}).
map { |piece| (piece =~ /\A\d+\z/) ? piece.to_i : piece }
}
end