Skip to content

Instantly share code, notes, and snippets.

@fj
fj / gist:2840961
Created May 31, 2012 04:10
DRYer way to do this route-drawing?
MyApp::Application.routes.draw do
resources :fruits
resources :vegetables
resources :sandwiches
resources :desserts
resources :meats
resources :baskets do # DRYer way to do this?
resources :fruits # repeated route! :(
resources :vegetables # repeated route! :(
@fj
fj / gist:2778358
Created May 23, 2012 22:57
Spot the subtle error
class CountrySearch
# Return the phone country code matching a particular
# two-letter country code (e.g. 'FR' will get France's
# country code, which is 33).
def self.country_code_for_alpha2(code)
return ArgumentError unless code =~ /^..$/
return nil unless c = Country[code]
c.country_code
end
@fj
fj / gist:2629588
Created May 7, 2012 18:41
Dynamic CSS in Rails 3.2

You operate a terrible clone of about.me called my.name. The entire purpose of the site is to show a user's name with some limited styling customization.

You allow users to configure a small number of parameters -- say, background color, font size, and some other properties -- and you store this information with each user.

When a request is made for my.name/123, you look up the user with id 123 and show that name in the middle of the page. The customized styling associated with each user is in the form of a CSS file that looks like styles.css?user_id=123. The CSS served is the same for any page; only the values for a few of the fields will be different for a given user_id.

You're using Rails 3.2 and Compass, so you want styles.css to actually be a dynamic file that generates an asset in the pipeline and which can be easily cached (so that you're not constantly recomputing styles.css?user_id=... for a given user).

What do you do?

let(:address_model) do
Class.new do
include ActiveModel
include ActiveModel::Validations
include HasAddress
def initialize(attributes = {})
@attributes = attributes
end
@fj
fj / gist:2568223
Created May 1, 2012 14:19
early-returns
class Poll
def confidence_interval
return nil unless enough_samples?
results.inject(ConfidenceMetric.new) do |cm, r|
cm.assess r
end
end
end
@fj
fj / gist:2344865
Created April 9, 2012 17:28
Vote: where does a "show all" link go?

You're building an assets inventory for your company.

A common operation is to look at a specific person, place, or asset to get more detailed information about it, and then go back to the list of all the things of that kind.

Where should you put the "show all" link? Please vote below for (a), (b), or (c).

  • Choice A: Near the title of the item you're viewing?
  _________      _________      _________
@fj
fj / gist:2237164
Created March 29, 2012 12:48
Pivotal Tracker workload report summary
* GET ... /projects/$id/memberships
-- get names by looking at XPath chunks matching //memberships/membership/person/name
* map each name to the string `mywork:"$name"`, which will be the search filter
* map each filter string to its HTML-encoded version
* for each name, GET ... /projects/$id/stories?filter=$filter
@fj
fj / gist:1942091
Created February 29, 2012 16:16
Exercise 43-1

Fruit baskets

Here is a Fruit class. Each fruit has a name and a number of calories.

class Fruit
  def initialize(fruit_name, calories)
    @fruit_name = fruit_name
    @calories = calories
  end
@fj
fj / gist:1770000
Created February 8, 2012 14:29
Rough estimate of Adobe Flash's lifetime energy consumption

First, some back-of-the-envelope assumptions. Take these with a grain of salt!

  • Install penetration: Flash claims 97% install penetration on the market.

  • Timespan: Flash was introduced in 1996. Let's assume it didn't hit that market penetration until 2002, and we'll ignore all the years before that.

  • Internet user base: The total number of people that can reach the Internet right now is about 2.25 billion. However, it only had about 350 million people in 2000. So let's say that on average the Internet had 1 billion people using it over the timespan in question.

  • Power usage: Typical desktop quad-core CPU power usage at maximum utilization: 70 to 100 W. Let's be conservative and say that at most, 25 W of that power is dissipated because of Flash (since at least some of it will be consumed by browsers, the OS, other programs, etc.).

@fj
fj / gist:1597544
Created January 11, 2012 23:51
Slightly nicer way of writing large files to disk with PHP
// Copy big file from somewhere else
$src_filepath = 'http://example.com/all_the_things.txt'; $src = fopen($src_filepath, 'r');
$tmp_filepath = '...'; $tmp = fopen($tmp_filepath, 'w');
$buffer_size = 1024;
while (!feof($src)) {
$buffer = fread($src, $buffer_size); // Read big file/data source/etc. in small chunks
fwrite($tmp, $buffer); // Write in small chunks
}