Skip to content

Instantly share code, notes, and snippets.

View deepak's full-sized avatar

Deepak Kannan deepak

View GitHub Profile
@deepak
deepak / benchmark_env.rb
Created November 22, 2010 11:51
setup in Benchmark like Test::Unit
#https://gist.github.com/709860
require 'benchmark'
# does Benchmark::bmbm have environments/setup like Test::Unit
# want some variables to be set per benchmark case - now have to dup them myself
# is is needed as the testcases have destructive operations, otherwise the testcases will be dependent on each other
def benchmark_setup(obj=TOP_SELF)
code = <<-CODE
@deepak
deepak / gist:729939
Created December 6, 2010 06:08
question regarding is_a? and kind_of?
# running on irb
# why is is_a? and kind_of methods work as expected on instances and not on the class itself
# --- bcuz Foo.class is a Class not a String
# how to differentiate between inheritance and composition relationship
module Baz
end
class Bar < String
end
@deepak
deepak / reserved_function.rb
Created December 6, 2010 08:07
a reserved function - to guard against the definition of a common function
# objective is to guard against a definition of a function
# eg. a common error is to define 'def included' rather than 'def self.included'
# also can i have a 'def self.included' whenever 'def included' - with the same content
class Object
def self.method_added(p1)
puts "== method_added: #{p1.inspect} | #{p1.class}"
raise "common_bug: maybe you want to use self.included rather than included" if p1 == :included
end
end
@deepak
deepak / _add.html.erb
Created December 7, 2010 12:24
toplevel view file
<div>
<%= link_to_function('DEL') do |page|
page.remove "nest"
end %>
</div>
@deepak
deepak / singleton_foo.rb
Created December 9, 2010 12:51
defining a singleton method on a attribute of an instance of a class
# defining a singleton method on a attribute of an instance of a class
# which method is recommended, define_singleton_methods is short-and-sweet. benchmark.
class FooNestedDef
attr_accessor :info
def initialize
@info = []
# NOTEL mindbending - def inside a def works. is this legal?
@deepak
deepak / gist:737196
Created December 11, 2010 06:30 — forked from anonymous/gist:734122
forked bcuz it was anonymous (could it be deleted .. dunno)
~/projects/jruby ➔ jmap -histo 19907 | grep rubyobj | head -50
116: 1945 46680 rubyobj.Gem.Version
118: 1898 45552 rubyobj.Gem.Version
120: 1826 43824 rubyobj.Gem.Requirement
123: 1804 43296 rubyobj.Gem.Requirement
158: 914 21936 rubyobj.Gem.Dependency
166: 876 21024 rubyobj.Gem.Dependency
209: 463 11112 rubyobj.Gem.Specification
211: 455 10920 rubyobj.Gem.Specification
319: 142 3408 rubyobj.ActiveSupport.TimeZone
@deepak
deepak / self_extend.rb
Created December 12, 2010 11:58
testing how extending self behaves and module_function
#!/usr/bin/env ruby
# USAGE:
# irb -rself_extend.rb
# > reload
require 'test/unit'
def reload
puts "Loading: #{__FILE__} ..."
load __FILE__
@deepak
deepak / prefix_plus.rb
Created December 12, 2010 14:49
Crappy non-functional code. tried to add a prefix addition to fixnum like the c operator ++<var> and <var>++
# tried to add a prefix addition to fixnum like the c operator ++<var> and <var>++
# asked by linusoleander on #ruby irc, discussed with apeiros, banisterfiend and My_Hearing
# as fixnum is a kind of constant, the idea was to store one value but use and display another
class Fixnum
# there is no intialize method in fixnum so cannot hook @actual_value to initilization
# also += 1 is a keyword so cannot hook assignment to a variable into the fixnum
# would have been possible if ruby had a repl (reader macros) like lisp
def pre_plus
@actual_value ||= (pre = self)
@deepak
deepak / benchmark_array_membership.rb
Created December 14, 2010 10:20
benchmark array membership in REE 1.8.7 2010-04-19 patchlevel 253 - 2010.02
# benchmark array membership in REE 1.8.7 2010-04-19 patchlevel 253 - 2010.02
# the basic access-pattern is like Array#include? - a big array and to check if a a smaller/single-object
# is included in the bigger one
# TODO: why is such a big difference between the two - irrespecive of the fact that both of them trying to do diff things
# array_intersect is checking if an array containing a single element is included? in a bigger array
# arr_ele_include is checking if an single integer array is included? in a bigger array
# array_intersect is the fastest followed by arr_ele_include
@deepak
deepak / proc_binding_size.rb
Created December 15, 2010 06:13
how to find the size of a binding - less is good - can i be selective
str = "hello"
#c=0
#p = Proc.new { str.size }
#p.binding.eval("ObjectSpace.each_object { c +=1 }")
def enclose_binding(*attr)
p = Proc.new { attr[0].size }
code = <<-CODE
class_ivars = self.class.instance_variables || []