Here goes :-
files_with - Find files containing a string inside the current directory, recursively
files_with() { grep -H -n "$*" ./* -R | cut -d: -f1 -f2; }
$ files_with hello ./blah.txt:4 ./sub/blah.txt:6
files_with() { grep -H -n "$*" ./* -R | cut -d: -f1 -f2; }
$ files_with hello ./blah.txt:4 ./sub/blah.txt:6
=render "shared/cat_box", :locals => {:cat => @cat}
ActionView::Template::Error (undefined local variable or method `cat' for #<#<Class:0x1062afad8>:0x1062aa240>)
=render :partial => "shared/cat_box", :locals => {:cat => @cat}
class DynamicRow
def self.with_keys(keys)
result = Class.new
result.class_eval do
keys.each { |k| attr_accessor k }
def initialize(hash)
hash.each { |k, v| instance_variable_set :"@#{k}", v }
end
end
result
end
def self.benchmark
require 'benchmark'
sample_hash = {:a => "hello", :b => 3}
Benchmark.bm do|b|
b.report("OpenStruct ") do
50_000.times { OpenStruct.new(sample_hash) }
end
b.report("DynamicRow ") do
row_type = DynamicRow.with_keys(sample_hash.keys)
50_000.times { row_type.new(sample_hash) }
end
end
end
end
user system total real OpenStruct 13.400000 0.080000 13.480000 ( 13.572973) DynamicRow 0.570000 0.010000 0.580000 ( 0.584201)
user system total real OpenStruct 25.750000 0.160000 25.910000 ( 26.117587) DynamicRow 0.790000 0.010000 0.800000 ( 0.809647)