Skip to content

Instantly share code, notes, and snippets.

View tkrajcar's full-sized avatar

Tim Krajcar tkrajcar

View GitHub Profile
@tkrajcar
tkrajcar / newrelic-metaprogramming-challenge.md
Created November 21, 2014 18:56
New Relic Ruby Metaprogramming Challenge

Your challenge, should you accept it, is to write a Ruby library that will modify an existing program to output the number of times a specific method is called.

You solution library should be required at the top of the host program, or via ruby's -r flag (i.e. ruby -r ./solution.rb host_program.rb).

Your solution library should read the environment variable COUNT_CALLS_TO to determine the method it should count. Valid method signatures are Array#map!, ActiveRecord::Base#find, Base64.encode64, etc.

Your solution library should count calls to that method, and print the method signature and the number of times it was called when the program exits.

Also, your solution should have a minimal impact on the program's running time. set_trace_func is a no-go...

def robots
robots = File.read(Rails.root + "config/robots.#{Rails.env}.txt")
render :text => robots, :layout => false, :content_type => "text/plain"
end
- flash.each do |name, msg|
- if msg.is_a?(String)
%div{:class => "alert alert-#{name == :notice ? "success" : "error"}"}
%a.close{"data-dismiss" => "alert"} ×
= content_tag :div, msg, :id => "flash_#{name}"
@tkrajcar
tkrajcar / gist:1181436
Created August 30, 2011 17:30
ruby 1.8 open-uri timeout example
begin
timeout(2) do
# do whatever
parsed = JSON.parse(open("http://derp.com/derpservice").read)
end
end
rescue Timeout::Error
logger.debug "Timed out."
end
begin
timeout(2) do
# do whatever
parsed = JSON.parse(open("http://derp.com/derpservice").read)
end
end
rescue
logger.debug "Timed out."
end