Skip to content

Instantly share code, notes, and snippets.

# If you like f'ing around with people using your code, you can do
def stupid(arg, newval, b)
lvars = eval("local_variables",b)
relevant = lvars.find { |lvar| eval(lvar, b).equal?(arg) }
Thread.current[:stupid] = newval
relevant.each do |lvar| eval("#{lvar} = Thread.current[:stupid]", b) end
end
a = 1
stupid(a, 2, binding)
@apeiros
apeiros / gist:377590
Created April 24, 2010 11:09
Baretests's systematic of last-run-states
Last Run States:
================
* run
* success
* aborted
* manually (? better term ?)
* skip
* skip-tagged
* component missing (:use => foo, foo was not found)
@apeiros
apeiros / irb_drop.rb
Created April 24, 2010 16:58
Drop into irb from anywhere within your code
module Kernel
# usage:
# require 'irb_drop'
# ...do some stuff...
# irb_drop(binding) # irb will open here with the current local variables
# ...continue doing stuff...
def irb_drop(context=nil, *argv)
require 'irb'
require 'pp'
require 'yaml'
class String
Base32Alphabet ||= ('A'..'Z').to_a+%w[2 3 4 5 6 7]
def base32
binary = unpack("B*").first
binary << "0"*(5-(binary.length % 5))
decoded = binary.scan(/.{1,5}/).map { |r| Base32Alphabet[r.to_i(2)] }.join('')
decoded << "="*(8-(binary.length.div(5) % 8))
decoded
end
end
## reversesortby.rb
class ReverseSortBy
include Comparable
attr_reader :value
def initialize(value)
@value = value
end
def <=>(other)
# form_for @model, :builder => FormBuilderExtension do |f| ...
module FormBuilderExtension
def new(*a)
builder = ActionView::Base.default_form_builder.new(*a)
builder.extend(self)
builder
end
end
@apeiros
apeiros / pool.rb
Created August 26, 2010 21:10
Thread::Pool
require 'thread'
class Thread
# Example:
# pool = Thread::Pool.new(10) do |exception| handle(exception) end
# pool.execute(1,2,3) do |x,y,z| whatever(x,y,z) end
# pool.join
class Pool
unless YAML.respond_to?(:dump_file) then
def YAML.dump_file(path, content)
File.open(File.expand_path(path), 'wb') do |fh|
fh.write(content.to_yaml)
end
end
class Object
def to_y(path=nil)
path ? YAML.dump_file(path, self) : to_yaml
@apeiros
apeiros / gist:634082
Created October 19, 2010 12:01
Terminate current process the hard way (kill -9)
module Kernel
# Terminate the current process - the hard way
def t!
`kill -9 #{$$}`
end
module_function :t!
end
@apeiros
apeiros / examples.rb
Created January 15, 2011 23:19
A generic smaller/bigger implementation, for things like infinite ranges etc.
(1..Bigger).first(5) # => [1, 2, 3, 4, 5]