Skip to content

Instantly share code, notes, and snippets.

View ridiculous's full-sized avatar

Ryan Buckley ridiculous

  • California
View GitHub Profile
module Usable
def config
@config ||= Config.new
end
def use(mod, options = {})
send :include, mod unless self < mod
if block_given?
yield config
else
# Original
name, categories = if product.categorie_names.present?
prod_categories = product.category_names.split('>')
[prod_categories.pop, prod_categories]
end
# Refactored using coercion and Ruby's pattern matching
*categories, name = *product.categorie_names.to_s.split('>')
@ridiculous
ridiculous / scratch_n_go.rb
Last active December 11, 2015 02:08
Looks for a correlation between method call time and # classes in the object space
require 'benchmark'
class ScratchNGo
#= Class Description
# Looks for a correlation between method call time and # classes in the object space. We do this by creating an initial
# sample size of new classes, each defined with the same number of methods. We then benchmark the call time for all methods of the
# original sample size of classes. We then create additional sets of class samples, measuring the call time of the original
# set each time.
def initialize(args)
@class_size = (args.pop || 5_000).to_i
@ridiculous
ridiculous / sexp.rb
Last active November 23, 2015 06:42
# if (me == 'ryan' and you == 'foo') || dude
# call_foo you
# elsif you == 'bar'
# call_bar you
# else
# puts "don't know who to call"
# return 'wut'
# end
s(:if,
#
# AUTOMATICALLY GENERATED SPEC
#
require "spec_helper"
describe UsersController do
subject { described_class.new }
describe "#index" do
describe DRG::Ruby::Condition do
let(:sexp) {}
subject { described_class.new sexp }
describe "#initialize" do
end
describe "#short_statement" do
context "if @statement =~ /(unless|if)(.+)$/ then" do
@ridiculous
ridiculous / ruby.v.elixir
Created November 4, 2015 20:43
Ruby vs Elixir
# = Description
# Loads all words in the dictionary into memory and finds the longest word
#
# Elixir
File.read!("/usr/share/dict/words") |> String.split |> Enum.max_by(&String.length/1) |> IO.puts
#=> formaldehydesulphoxylate
# Time (after compiled):
# real 0m1.887s
# user 0m1.722s
# sys 0m0.247s
#= Should be able to handle phone numbers with varying lengths
# length of 11
format_number('18083334444') #=> '+1 (808) 333-4444'
# length of 12
format_number('639173334444') #=> '+63 (917) 333-4444'
# length of 13
format_number('6140403334444') #=> '+614 (040) 333-4444'
# length of 14
format_number('00447743334444') #=> '+0044 (774) 333-4444'
# config/initializers/quiet_assets.rb
class QuietAssets
def initialize(app)
@app = app
end
def call(env)
original_level = Rails.logger.level
if env['PATH_INFO'] =~ %r{\A/assets/}
Rails.logger.level = Logger::WARN
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails', branch: 'master' # master against ref "f1f0a3f8d99aef8aacfa81ceac3880dcac03ca06"