Skip to content

Instantly share code, notes, and snippets.

@crazymykl
crazymykl / foo_helper.rb
Created June 7, 2012 23:43
A quickie to escape metacharacters in strings for use in jQuery selectors
def jqe(str)
str.gsub( /[!"\#$%&'()*+,.\/:\\;<=>?@\[\]^`{|}~]/, '\\\\\\\\\&')
end
@crazymykl
crazymykl / document_uploadr.rb
Created December 26, 2012 16:44
The uploader showing carrierwaveuploader/carrierwave#937. I would like the version :pdf (only) to have different fog attributes.
# encoding: utf-8
class DocumentUploader < CarrierWave::Uploader::Base
include ::CarrierWave::Backgrounder::Delay
include CarrierWave::MimeTypes
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
include CarrierWave::UNOConv
@crazymykl
crazymykl / gist:5001155
Created February 21, 2013 01:18
Undocumented ruby
The following items are not documented:
class ARGF # is documented
# in file io.c:11364
# +ARGF.argv->ARGV+ is not documented
@crazymykl
crazymykl / gist:5001237
Created February 21, 2013 01:41
patch to let rdoc -C1 parse trunk
diff --git a/lib/profiler.rb b/lib/profiler.rb
index be33daf..22f3490 100644
--- a/lib/profiler.rb
+++ b/lib/profiler.rb
@@ -76,7 +76,7 @@ module Profiler__
@@start = nil # the start time that profiling began
@@stacks = nil # the map of stacks keyed by thread
@@maps = nil # the map of call data keyed by thread, class and id. Call data contains the call count, total time,
- PROFILE_CALL_PROC = TracePoint.new(*%i[call c_call b_call]) {|tp| # :nodoc:
+ PROFILE_CALL_PROC = TracePoint.new(:call, :c_call, :b_call) {|tp| # :nodoc:
@crazymykl
crazymykl / foo.rb
Created May 28, 2013 16:05
Make Ruby class methods act as staticmethod in Python.
class Foo
extend Forwardable
def_delegators :'self.class', :foo, :bar
def self.foo
:foo
end
def self.bar
'bar'
@crazymykl
crazymykl / fizzbuzz.rb
Last active December 18, 2015 00:09
FizzBuzz, lambda edition
#! /usr/bin/env ruby
class FizzBuzz
attr_reader :limit, :matchers
DEFAULT_LIMIT = 100
DEFAULT_MATCHERS = [
-> x { "Fizz" if (x % 3).zero? },
-> x { "Buzz" if (x % 5).zero? },
@crazymykl
crazymykl / gist:7568756
Created November 20, 2013 18:49
Implement a generalized FizzBuzz, FooBaz, such that FooBaz((1..100), {3 => "Fizz", 5 => "Buzz"}) is equivalent to FizzBuzz.
class FooBaz
attr_reader :replacements, :range
def initialize range, replacements
@range = range
@replacements = replacements
end
def call
range.each do |number|
- 100.times do
%p/
DIGIT_VALUES = {
'I' => 1,
'IV' => 4,
'V' => 5,
'IX' => 9,
'X' => 10,
'XL' => 40,
'L' => 50,
'XC' => 90,
'C' => 100,
@crazymykl
crazymykl / count_queries.rb
Created February 9, 2016 15:32
A way to count the number of queries the passed block causes.
def count_queries(&blk)
x = 0
counter = -> (*args) { x += 1 }
ActiveSupport::Notifications.subscribed counter, 'sql.active_record', &blk
x
end