This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<%= link_to_function('DEL') do |page| | |
page.remove "nest" | |
end %> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# USAGE: | |
# irb -rself_extend.rb | |
# > reload | |
require 'test/unit' | |
def reload | |
puts "Loading: #{__FILE__} ..." | |
load __FILE__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || [] |