Skip to content

Instantly share code, notes, and snippets.

{{#bs-page-heading verbose=true title="Welcome back!" subtitle=(
format-message (intl-get 'messages.dashboardOverview.subtitle')
percentChange=(format-number averageChargePercentChange style='percent')
previous=(format-currency previousAverageCharge currency=currency)
current=(format-currency currentAverageCharge currency=currency)
sign=(compute-sign averageChargePercentChange bufferedFractionDigits=2)
timespan=(format-message (intl-get 'messages.helpers.timeElapsed')
unitCount=(time-elapsed overviewTimespan unit='months') unit='months')
)
}}
@wangjohn
wangjohn / README.md
Last active December 28, 2015 01:29
Boston Cab Visualization

Boston Cab Visualization

This makes a heatmap of cab pick-ups for a given time of day. You can select different ranges of time using the slider.

Questions

  1. What is the message in your visualization and what techniques did you use to illustrate it?

The message in my visualization is that taxi pickup locations change depending on the time of day. It tends to be the case that in the mornings, people get picked up in Kenmore, North Station, and in Back Bay. However, in the evening, taxis tend to pick people up near the Prudential Center, the Financial District, and near South Station.

@wangjohn
wangjohn / gist:6055413
Created July 22, 2013 16:42
Performance Benchmark for Refactoring of class_attribute Method
require 'active_support/core_ext/class/attribute.rb'
require 'benchmark'
NUM_TRIALS = 1000000
Benchmark.bm(25) do |x|
x.report("Defining attribute") do
NUM_TRIALS.times do
class MyClass
class_attribute :my_attribute
@wangjohn
wangjohn / gist:5542610
Created May 8, 2013 18:43
Benchmark for define_model_callbacks
require 'active_model'
require 'benchmark'
class Post
extend ActiveModel::Callbacks
define_model_callbacks :make
def make
run_callbacks :make do
end
@wangjohn
wangjohn / gist:5527820
Created May 6, 2013 20:15
Presentation Code
module ActiveRecord
  extend ActiveSupport::Autoload

  autoload :Base
  autoload :Callbacks
  autoload :Core
  ...
end
@wangjohn
wangjohn / gist:5505347
Last active December 16, 2015 22:09
Callbacks performance

activerecord/test/cases/associations_test.rb

On master (when scanning all of the callbacks):

  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
  1.85     9.26      0.82    18412     0.04     0.05  ActiveSupport::Callbacks::Callback#matches?
  1.22    12.32      0.54    18412     0.03     0.08  ActiveSupport::Callbacks::Callback#duplicates?
  0.93    14.61      0.41    19600     0.02     0.21  ActiveSupport::Callbacks::CallbackChain#remove_duplicates
@wangjohn
wangjohn / gist:5418350
Last active September 14, 2021 08:53
Rendering Partials in Rails

Let's look at what happens when you do something like this inside of a view:

<%= render partial: 'some_partial' -%>

To render a partial, Rails has a helper method inside of Action View. The render method serves as a delegator and takes its input arguments and provides them to the view_renderer object.

The view_renderer object is initialized when ActionView::Base is initialized and is of the ActionView::Renderer class. This class performs all of the rendering that is necessary inside of the views. The design pattern of delegating to an object is used. When you call view_renderer.render, you are asking the view_renderer object to delegate the arguments of your call to another more specific object.

@wangjohn
wangjohn / gist:5409937
Created April 18, 2013 03:45
Error Message for Installing Postgres Gem
[Wed Apr 17 - 23:43:17]
john@ubuntu:~/taskmail
$ gem install pg
Building native extensions. This could take a while...
ERROR: Error installing pg:
ERROR: Failed to build gem native extension.
/home/john/.rvm/rubies/ruby-2.0.0-p0/bin/ruby extconf.rb
checking for pg_config... yes
@wangjohn
wangjohn / gist:5308220
Created April 4, 2013 06:09
Assertion Checking
class StrawMan
  attr_accessor :assertion_log

  @assertion_log = []

  class << self
    def check_assertions(name, &assertion)
      alias_method :some_crazy_uuid, name
@wangjohn
wangjohn / gist:5304371
Last active December 15, 2015 18:29
Static-Like Checking in Rails Tests

My idea is to create something in rails where you can do checking on your rails app which is similar to statically-typed languages. When you define a method, you can specify asserts on the inputs and outputs. Imagine that @return is the return value of the method, then you could write:

#
# assert ids.all? do { |id| id.kind_of?(Integer) }
# assert @return.kind_of?(ActiveRecord)
# 
def find(*ids)
  ...
end