Skip to content

Instantly share code, notes, and snippets.

View deepak's full-sized avatar

Deepak Kannan deepak

View GitHub Profile
@deepak
deepak / test_controller.rb
Created August 30, 2010 08:37
To benchmark ActiveRecord connection-pool size
class TestController < ApplicationController
# To benchmark ActiveRecord connection-pool size
# connection pool 5
def ping
t1 = Time.now
1.upto(10) { Product.find(Integer(rand*1000)) }
time_taken = (Time.now - t1)
render :text => "time taken: #{time_taken.inspect}"
end
@deepak
deepak / redis_schema.txt
Created September 28, 2010 11:42
how to name keys in redis for keyvalue stores
# how to name keys in redis for keyvalue stores
http://code.google.com/p/redis/wiki/TwitterAlikeExample
http://rediscookbook.org/introduction_to_storing_objects.html
http://www.slideshare.net/playnicelyapp/redis-schema-design-for-playnicely-redis-london-meetup
antirez has a book about keyvalue design in the pipeline
http://code.google.com/p/redis/wiki/IntroductionToRedisDataTypes
schema:
<namespace>:<globalObject>
# ri does not show private methods
# why and any way to force it to index and show private methods
ri Date#hour
ri DateTime #hour is a private method
ri DateTime#hour #is not shown
@deepak
deepak / const_lookup.rb
Created October 11, 2010 10:35
ruby_constant_lookup_cache_benchmark.rb
# ruby caches the const lookup like method lookup
# due to this no need to explicitly memonize in code
# although, i think eval will break this cache
# TODO: what will clear the const/method cache
# http://yehudakatz.com/2010/02/25/rubys-implementation-does-not-define-its-semantics/
require 'benchmark'
LOGGER = 10
@deepak
deepak / test_proc.rb
Created October 11, 2010 11:02
block_calling_syntax.rb
# -*- coding: utf-8 -*-
# http://yehudakatz.com/2010/02/25/rubys-implementation-does-not-define-its-semantics/
# Nathan de Vries, Posted February 25, 2010, 2:27 am
# From PickAxe:
# “If the last argument to a method is preceded by an ampersand, Ruby assumes that it is a Proc object. It removes it from
# the parameter list, converts the Proc object into a block, and associates it with the method.”
# Hence being referred by some as the “blockinator”. i.e. take the argument prefixed by “&” and convert it to a block so that
# the receiver can invoke it via “yield”. This idea of conversion to a block is what steered people towards overriding
# to_proc (for better or worse).
@deepak
deepak / foo.rb
Created October 13, 2010 10:17
ruby metaprogramming usage using define_method and undef_method.txt
class Foo
puts "define_method: #{defined?(define_method).inspect}"
define_method(:bar) { puts "one" }
end
a = Foo.new
a.bar
class Foo
class << self
@deepak
deepak / outout_of_rvm_debug.txt
Created October 21, 2010 03:48
only the rake bin has an extra dir "foo" on it
deepak@octo:~$ rvm debug
ree-1.8.7-2010.02:
/home/deepak/.rvm/scripts/info: line 12: zsh: command not found
rvm 1.0.0 by Wayne E. Seguin ([email protected]) [http://rvm.beginrescueend.com/]
ree-1.8.7-2010.02:
system:
uname: "Linux octo 2.6.32-25-generic #44-Ubuntu SMP Fri Sep 17 20:05:27 UTC 2010 x86_64 GNU/Linux"
zsh: ""
@deepak
deepak / ruby_thread_parent.rb
Created November 1, 2010 11:30
keep track of the parent of a ruby thread.rb
T0 = Thread.main
t1 = Thread.new(Thread.current) { |x| Thread.current[:parent] = x; sleep 1_00_000; }
t2 = Thread.new(Thread.current) { |x| Thread.current[:parent] = x; sleep 1_00_000; }
(t1[:parent] == t2[:parent]) && (t1[:parent] == T0)
__END__
# to find out the parent of a thread, ie. was spawned by which thread
@deepak
deepak / rvm_rails3_args_error.txt
Created November 8, 2010 13:44
problem in running rails3 server
> type rvm | head -n1
=> rvm is a function
> rvm debug > r.out 2> r.err.out
> cat r.out
=>
ree-1.8.7-2010.02:
rvm 1.0.0 by Wayne E. Seguin ([email protected]) [http://rvm.beginrescueend.com/]
ree-1.8.7-2010.02:
@deepak
deepak / ar_conditions_overkill.rb
Created November 22, 2010 08:14
went on a refactoring/beautification overdrive just so that the lines are under 80 columns
# went on a refactoring/beautification overdrive just so that the lines are under 80 columns
# ruby statements that overflow into the next row are an abomination :-)
# mangled production-code to remove const-names. It is not production or even code which has been run before
# please refactor!!
# * Any better way to make sure the long condition string is properly formatted
# * the gsub(/\s/, '') is also suspect as it would replace spaces etc in a proper sql
Comment.count(:id,
:include => [:person, :had_commented],