Skip to content

Instantly share code, notes, and snippets.

View njonsson's full-sized avatar
🤝
Working @amzn but open to new opportunities

Nils Jonsson njonsson

🤝
Working @amzn but open to new opportunities
View GitHub Profile
@njonsson
njonsson / git-rebase-tags.rb
Created April 7, 2011 18:29
UPDATE: Use the `--tag-name-filter` option of `git-filter-branch`. http://blog.nilsjonsson.com/post/4421450571/rebasing-tags-in-git-repositories
#! /usr/bin/env ruby
def pluralize(word, count=2, plural_word=nil)
plural_word ||= "#{word}s"
"#{count} #{(count == 1) ? word : plural_word}"
end
unless (ARGV.length == 1) &&
(good_revision = system("git log -1 #{ARGV.first} 2>/dev/null"))
puts "Unknown revision '#{ARGV.first}'" unless good_revision
@njonsson
njonsson / specifying-default-values-with-rubys-module-attr-reader-and-attr-accessor.md
Last active May 29, 2021 12:33
Specifying default values with Ruby’s Module#attr_reader and #attr_accessor

[This is [a DZone Snippet I created in August 2007][dzone-snippet]. DZone Snippets is now a defunct site, so I’m reposting it here.]

Dependency injection in Ruby is as easy as falling off a log. As [Jamis Buck has pointed out][jamis-buck-net-ssh-revisited], DI is a good thing, but DI frameworks for Ruby are overkill. The language makes them unnecessary.

Here's how to enhance [Module#attr_reader][ruby-module-attr-reader] and [#attr_accessor][ruby-module-attr-accessor] so that they can receive an options hash for specifying the default value of an attribute.

module AttrWithDefaultExtension
  
  module ClassMethods
@njonsson
njonsson / The difference between the junior and senior Rails developer
Created May 6, 2009 03:31
The difference between the junior and senior Rails developer — an extract of the discussion forum at the Rubyists LinkedIn group
Someone recently asked the following question in the discussion forum of the Rubyists LinkedIn group: What separates a junior Rails developer from a senior one?
My response follows. Join us at http://www.linkedin.com/groups?gid=120725 to weigh in on this and other topics of interest to Rubyists. As of today there are almost 1,200 members, including numerous movers and shakers in the Ruby and Rails communities.
“Distinguishing between junior and senior people in the Rails world is not so different from making the distinction in other web development environments.
“Junior Rails people have not dealt with scaling issues to the degree that senior people have. Getting a public-facing Rails application to perform under significant stress is more challenging than doing the same with other building materials such as PHP. Senior people know how to performance-test Rails applications, where to look for bottlenecks, and how to eliminate them one after another until performance is acceptable in real conditions. The Ra
// (Do this up front somewhere else.)
var closeMethod = typeof(OpenNETCF.Phone.Sim.Sim).GetMethod("Close");
// (Do this many times.)
using (var disposer = new Disposer<OpenNETCF.Phone.Sim.Sim>(OpenNETCF.Phone.Sim.Sim(), closeMethod))
{
// Use the 'disposer.Object' property ...
}
using System;
using System.Diagnostics;
/// <summary>
/// Serves as a wrapper around objects that require disposal but that do not
/// implement <see cref="System.IDisposable"/>.
/// </summary>
/// <typeparam name="T">The type of <see cref="Object"/></typeparam>
public abstract class DisposerBase<T> : IDisposable
{
var sim = new OpenNETCF.Phone.Sim.Sim();
try
{
// Use the 'sim' variable ...
}
finally
{
sim.Close();
}
using (var disposer = new Disposer<OpenNETCF.Phone.Sim.Sim>(new OpenNETCF.Phone.Sim.Sim(), "Close"))
{
// Use the 'disposer.Object' property ...
}
// Unfortunately, this isn't possible.
using (var sim = new OpenNETCF.Phone.Sim.Sim())
{
// Use the 'sim' variable ...
}
source 'http://rubygems.org'
gem 'rake'
group :development do
gem 'autotest'
gem 'autotest-fsevent'
gem 'ruby-debug'
end
require 'sinatra'
get '/all-good' do
[200, [['Set-Cookie', 'foo=bar; baz']], 'Hello World!']
end
get '/huh' do
[404, 'What?']
end