Skip to content

Instantly share code, notes, and snippets.

View roniegh's full-sized avatar

Ronie Henrich roniegh

View GitHub Profile
@roniegh
roniegh / trace_variable_changes.rb
Last active February 10, 2017 08:43
Find where a variable is over-written with a different value
class Abc
def initialize
@thing = {"foo" => "bar"}
end
end
$stack = [{:config => {}}]
set_trace_func lambda { |event, file, line, id, binding, classname|
printf("%8s %s:%-2d %10s %8s\n", event, file, line, id, classname)
@roniegh
roniegh / proxy_hash.rb
Created July 28, 2015 23:29
Rails ActiveRecord 2.3.15 hash that supports procs as values (can be used with ActiveRecord default_scope)
module Proxies
# ProxyHash can be used when there is a need to have procs inside hashes, as it executes all procs before returning the hash
# ==== Example
# Proxies::ProxyHash.new(:company_id => lambda{logged_user.try(:company_id)})
class ProxyHash < ::Hash
instance_methods.each{|m| undef_method m unless m =~ /(^__|^nil\?$|^method_missing$|^object_id$|proxy_|^respond_to\?$|^send$)/}
def [](_key)
call_hash_procs(@target, @original_hash)
ProxyHash.new(@original_hash[_key])