Skip to content

Instantly share code, notes, and snippets.

View leejarvis's full-sized avatar

Lee Jarvis leejarvis

View GitHub Profile
#!/usr/bin/env ruby
# Simple configuration
require 'singleton'
class Foo
class Config < Struct.new(:first_name, :last_name)
include Singleton
end
module SqlRandom
module Relation
def shuffled
rand_cmd = if connection.adapter_name =~ /mysql/i
'rand()'
else
'random()'
end
clone.order(rand_cmd)
end
@leejarvis
leejarvis / blah
Created June 14, 2012 16:03
a bad example
#!/usr/bin/env ruby
module GroupSend
def group_send(methods, *calls)
items = []
Array(methods).each do |meth|
calls.each { |c| items << self.send(meth).send(c) }
end
#!/usr/bin/env ruby
class TryProxy
def initialize(object)
@object = object
end
def method_missing(meth, *args, &block)
if @object.respond_to?(meth)
@object.__send__(meth, *args, &block)
#!/usr/bin/env ruby
begin
require './config/environment'
rescue LoadError
abort 'Not in a rails environment?'
end
tables = ActiveRecord::Base.connection.tables
#!/usr/bin/env perl
use Data::Dumper;
use Slop;
my $opts = Slop->new();
$opts->on('v', 'version', sub { print 'version 1.0.0'});
$opts->on('n=', 'name=', 'Your name');
$opts->parse(@ARGV);
#!/usr/bin/env perl
use Data::Dumper;
my $opts = Slop->new();
$opts->on('v', 'version', sub { print 'version 1.0.0'});
$opts->on('n=', 'name=', 'Your name');
$opts->parse(@ARGV);
#!/usr/bin/env ruby
# dont try this at home
items = [{ a: 1, b: 2 }, { a: 1, b: 3 }, { a: 4, b: 5 }]
items = items.each_with_object(Hash.new { |h, k| h[k] = [] }) { |i, o| o[i[:a]] << i[:b] }.map { |k, v| [k, v.join(', ')] }
p items #=> [[1, "2, 3"], [4, "5"]]
# Slop version 3
commands = Slop::Commands.new do
banner "ruby foo.rb [options]\n"
on 'new' do
on '-F', '--force', 'Force creation'
on '--outdir=', 'Output directory'
end
# Slop version 3
require 'slop'
opts = Slop.optspec(<<-SPEC)
n,name= Your name
p,pass=? An optional password
A,auth Use authentication (requires password)
SPEC