This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Simple configuration | |
require 'singleton' | |
class Foo | |
class Config < Struct.new(:first_name, :last_name) | |
include Singleton | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SqlRandom | |
module Relation | |
def shuffled | |
rand_cmd = if connection.adapter_name =~ /mysql/i | |
'rand()' | |
else | |
'random()' | |
end | |
clone.order(rand_cmd) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
begin | |
require './config/environment' | |
rescue LoadError | |
abort 'Not in a rails environment?' | |
end | |
tables = ActiveRecord::Base.connection.tables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |