Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / deploy.rb
Created July 24, 2008 09:08
Using acts_as_solr with Monit and Capistrano
before :deploy, "solr:stop"
after :deploy, "solr:start"
namespace :solr do
task :stop, :roles => :app do
run "#{sudo} monit stop solr"
end
task :start, :roles => :app do
run "#{sudo} monit start solr"
@mudge
mudge / xml.rb
Created July 28, 2008 11:50
Fix for using acts_as_solr with libxml-ruby 0.8.1
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
@mudge
mudge / nameable.rb
Last active August 29, 2015 14:05
Add a "name" accessor to objects with first_name and last_name fields.
module Nameable
# Assuming first_name and last_name are accessors.
def name=(name)
self.first_name, *_, self.last_name = name.split
end
def name
[first_name, last_name].compact.join(" ")
end
@mudge
mudge / count.rb
Created July 29, 2008 10:37
A fast count method for Ruby 1.8 arrays.
class Array
def count(elem)
length - (self - [elem]).length
end
end
@mudge
mudge / averageable.rb
Created July 29, 2008 10:49
Methods to calculate the mean, median and mode of an array.
module Averageable
def mean(&block)
sum(&block) / length.to_f
end
def count(elem)
length - (self - [elem]).length
end
def mode
@mudge
mudge / count_benchmark.rb
Created July 29, 2008 11:10
Comparing two different implementations of a count method for arrays.
require 'benchmark'
class Array
def count_with_loop(elem)
enum = 0
each { |e| enum += 1 if elem == e }
enum
end
def count_without_loop(elem)
@mudge
mudge / gist:4088
Created August 5, 2008 15:50
I always forget this one.
$COMMAND_WITH_OUTPUT_TO_SUPPRESS >/dev/null 2>&1
@mudge
mudge / gist:4092
Created August 5, 2008 16:04
Install git's man pages.
GIT_VERSION=`git --version | awk '{print $3}'`
curl -O http://www.kernel.org/pub/software/scm/git/git-manpages-${GIT_VERSION}.tar.bz2
sudo tar xjv -C /usr/local/share/man -f git-manpages-${GIT_VERSION}.tar.bz2
@mudge
mudge / gist:6168
Created August 19, 2008 10:12
Delete files by a pattern
find /path_to_search -type f -name "*the_pattern*" -exec rm {} \;
@mudge
mudge / gist:7492
Created August 27, 2008 14:37
Methods to test whether an object is numeric or not.
class Object
# Test if the object is an integer.
#
# "54".is_i? #=> true
# "54_000".is_i? #=> true
def is_i?
!!Integer(self) rescue false
end