Skip to content

Instantly share code, notes, and snippets.

View deepak's full-sized avatar

Deepak Kannan deepak

View GitHub Profile
@deepak
deepak / ar_add_on_attr_or_base.rb
Created January 6, 2011 11:46
ActiveRecord rails 2.3.9 patch to add an error on the attribute or base otherwise
# ActiveRecord rails 2.3.9 patch to add an error on the attribute or base otherwise
module ActiveRecord
class Errors
def add_on_attr_or_base(attribute, message = nil, options = {})
if @base.respond_to?(attribute)
add(attribute, message, options)
else
ActiveRecord::Base.logger.warn "attribute #{attribute} does not exist so we are adding on base"
add_to_base(message)
end
@deepak
deepak / thread_kill.rb
Created January 14, 2011 09:24
Thread#kill works as a script but not in irb
t = Thread.new { kill }
puts t.inspect
# irb eval's code. but then this works as a script but does not work in irb
# eval "t = Thread.new { kill }; puts t.inspect"
__END__
runs successfully when run as a script ie. ruby <filename>
#<Thread:0x1dc5090 dead>
@deepak
deepak / thread_join_args.block_and_safe.rb
Created January 14, 2011 09:53
Thread#join will block until the thread exists. We can also pass a timeout to Thread#join. an upper bound for blocking the join call
#!/usr/bin/env ruby
# USAGE: ruby <file.rb>
threads = []
threads << Thread.new { sleep 10; puts "one" }
# does not work
# /tmp/bar.rb:12: undefined local variable or method `kill' for main:Object (NameError)
# from /tmp/bar.rb:18:in `join'
# from /tmp/bar.rb:18
# from /tmp/bar.rb:18:in `each'
# https://github.com/rubygems/rubygems-mirror/blob/master/lib/rubygems/mirror/pool.rb
# also check
require 'thread'
class MirrorPool
def initialize(size)
@size = size
@queue = Queue.new
end
@deepak
deepak / [TRIVIAL] thread_join_and_status.rb
Created January 14, 2011 12:12
[TRIVIAL] the thread becomes dead after we get the value. also we can pass a timeout value to Thread#join and try to join afterwards
t = Thread.new { sleep 5; puts 1 }
puts "status: #{t.status} on #{t.inspect}"
t.join(1)
puts "status: #{t.status} on #{t.inspect}"
t.join(1)
puts "status: #{t.status} on #{t.inspect}"
t.join(1)
puts "status: #{t.status} on #{t.inspect}"
t.join(1)
puts "status: #{t.status} on #{t.inspect}"
@deepak
deepak / exception_handeling.rb
Created January 19, 2011 06:22
Benchmarking exception handeling in ruby.
# throwing and catching an exception is worse than returning a simple type
# maybe it is because exceptions have to maintain the backtrace
# hypothesis: if the code throwing an exception is nested deep then the perf hit will be larger
# as the backtrace will be bigger
# the java and lisp communities have had discussions on exceptions before.
# The LISP guys are smug about the exception handling in their language. dunno why but they are smug about
# everything. smug lisp weenies...
require 'benchmark'
@deepak
deepak / if_case_benchmark.rb
Created January 20, 2011 10:41
benchmark btw if and case statements in ruby
# asked by dhruvasagar on irc
# http://stackoverflow.com/questions/2547059/delphi-performance-case-versus-if
require 'benchmark'
SO_MUCH = 10_000
X = 'f'
# 8 cases
Benchmark.bmbm do |x|
x.report("case") do
# is it a bad idea to override ruby core classes
# ex:
# 1. overriding functions which almost do the same thing. breaks the principle of surprise. we override
# Array#size but Array#length is the older definition.
# 2. overriding array so that the default position starts at 1 rather than 0 and other included modules
# like Emumerator#each just work. We can do it but it seems very cumbersome and fragile.
# some good practices given at: http://www.ruby-forum.com/topic/214157
# do not override core classes, either:
# 1. add it to the metaclass of the specific object
@deepak
deepak / groovy_it_operator.rb
Created January 21, 2011 10:31
groovy it operator in ruby
# goes in an infinite loop. does not work
class Array
def each(&blk)
enum = to_enum
if block_given?
it = enum.next
blk.eval("it=args")
yield
else
@deepak
deepak / wget-list.sh
Created January 24, 2011 06:48
wget-list: manage the list of downloaded files
#!/bin/sh
# wget-list: manage the list of downloaded files
# invoke wget-list without arguments
# http://www.linux.com/feature/59457
# http://ubuntuforums.org/showthread.php?t=672074
# http://www.lifehack.org/articles/lifehack/make-your-idle-computer-work-for-you.html
# http://decafbad.net/projects/scripts
# http://www.google.co.in/search?q=wget+queue&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a