View alias_task.rake
def alias_task(name, old_name)
t = Rake::Task[old_name]
desc t.full_comment if t.full_comment
task name, *t.arg_names do |_, args|
# values_at is broken on Rake::TaskArguments
args = t.arg_names.map { |a| args[a] }
t.invoke(args)
end
end
View db_redo.rake
def interesting_tables
rval = ActiveRecord::Base.connection.tables.sort
rval.reject! do |tbl|
['schema_migrations','schema_info', 'sessions', 'public_exceptions'].include?(tbl)
end
rval
end
namespace :db do
namespace :backup do
View howto-manually-add-trust-cert-to-rubygems.md

SSL upgrades on rubygems.org and RubyInstaller versions

UPDATE 2014-12-21: RubyGems 1.8.30, 2.0.15 and 2.2.3 have been released. It requires manual installation, please see instructions below.


Hello,

If you reached this page, means you've hit this SSL error when trying to

View email_validator.rb
# blog post: http://blog.slashpoundbang.com/post/12694519460/email-validation-in-rails-3-or-active-model-without
# http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
class EmailValidator < ActiveModel::EachValidator
# Domain must be present and have two or more parts.
def validate_each(record, attribute, value)
address = Mail::Address.new value
record.errors[attribute] << (options[:message] || 'is invalid') unless (address.address == value && address.domain && address.__send__(:tree).domain.dot_atom_text.elements.size > 1 rescue false)
end
end
View benchmark_downcase_vs_enum_downcase.rb
# "[A]pplying downcase once is better than calling it (possibly) multiple times?"--sawa
# via http://stackoverflow.com/questions/38162768/38163148#comment63754959_38163167
# "How to make a string 1st letter caps, 2nd letter noncaps, 3rd letter caps, 4th letter noncaps…?"
require "benchmark"
n = 1_000_000
Benchmark.bmbm do |x|
x.report("String#downcase.chars.map.with_index {|c,i| i%2==0 ? c.upcase : c}.join") do
n.times {
"Stackoverflow is awesome!".downcase.chars.map.with_index {|c,i| i%2==0 ? c.upcase : c}.join
View ruby-encryptor-exercise.md

Encryptor

Have you ever wanted to send a secret message?

The Plan

Today we use computers to mask messages every day. When you connect to a website that uses "https" in the address, it is running a special encoding on your transmissions that makes it very difficult for anyone to listen in between you and the server.

View gist:4a0a3ad089ce08f3476d025bea7361b4
# each_with_index, each_with_object and inject({}) are all %50 slower then #each
# simple each with an addition noop
[4] pry(main)> puts Benchmark.measure { (1..10_000_000).each { |i| i + 1 } }
0.450000 0.000000 0.450000 ( 0.448808)
# each_with_index
[5] pry(main)> puts Benchmark.measure { (1..10_000_000).each_with_index { |i, index| i + 1 } }
0.650000 0.000000 0.650000 ( 0.645812)
View benchmark_dead_horse.rb
require 'benchmark'
N = 1000000
nums = N.times.map{ rand(N) }
enum = [1, 2, 'string', {}, [], false, true, nil]
def process(v)
v
end
View gist:da94e895d50c6bf2d41c
@import "compass/css3/images";
// CSS-only multi-line ellipsis with generated content
// yields `position:relative`, so remember to declare an eventual `position:absolute/fixed` *after* including this mixin
@mixin limitLines(
$maxLinesPortrait, // Mandatory: The number of lines after which the clipping should take action.
$maxLinesLandscape: $maxLinesPortrait, // You may provide a different line limit for landscape orientation.
// Note that 'portrait' is our default orientation. However, if you omit $maxLinesLandscape,
// the value of $maxLinesPortrait is used for whatever orientation (that is, without a media query).
View gist:10936593
# Example of how to test a module included into multiple models
require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
describe Featurable do
class FeaturableObject
include Featurable
attr_accessor :sport, :featured_at