Skip to content

Instantly share code, notes, and snippets.

@tlux
tlux / stochastic_array_extensions.rb
Last active October 6, 2015 03:18
Stochastic Array Aggregation Methods
class Array
def average
self.reduce(:+) / length.to_f
end
def median
values = self.sort
if length % 2 == 0 # even
center = length / 2
(values[center - 1] + values[center]) / 2.0
@tlux
tlux / fixnum_extensions.rb
Last active October 6, 2015 12:08
Even/Odd Numbers
class Fixnum
def even?
(self % 2).zero?
end
def odd?
!even?
end
end
@tlux
tlux / gist:3414871
Created August 21, 2012 11:52
Combining Addressable::URIs
require 'addressable/uri'
module CombineUri
def combine(other_uri)
dup.combine!(other_uri)
end
def combine!(other_uri)
return self unless other_uri
other_uri = self.class.parse(other_uri) unless self.class == other_uri.class
@tlux
tlux / gist:3709895
Created September 12, 2012 21:00
Check string numericality
class String
def fixnum?
!/^[\d]+$/.match(self).nil?
end
def float?
!/^[\d]+\.[\d]+?$/.match(self).nil?
end
def numeric?
@tlux
tlux / pin_generator.rb
Last active October 12, 2015 21:08
PIN Generator that can generate single PINs or an Array with a specified count of unique PINs
class PinGenerator
class << self
def single(length)
chars = [('A'..'Z'), ('0'..'9')].map(&:to_a).flatten
chars.delete "O"
chars.delete "0"
chars.delete "I"
result = ""
prev_char = ""
while result.length < length do
def placehold_if(condition, options = {}, &block)
if condition
placeholder_class = options.fetch(:class, 'placeholder')
content_tag(:span, options[:with], class: placeholder_class) if options[:with]
else
capture(&block)
end
end
def placehold_unless(condition, options = {}, &block)
@tlux
tlux / hstore.rb
Last active December 13, 2015 17:28
PostgreSQL HStore Parser & Dumper
module HStore
class << self
def parse(str)
hash = Hash[*str.to_s.split(',').map do |item|
if item.strip =~ /\A\"(.*)\"[ ]*=>[ ]*(.*)\z/
key = $1.to_sym
value = $2 =~ /\"(.*)\"/ ? $1 : nil
[key, value]
else
raise ArgumentError, 'invalid format'
@tlux
tlux / comparison_validator.rb
Last active December 16, 2015 09:29
Compare Validator (ActiveModel)
# validates :password_approval, comparison: { to: :password }
# - or -
# validates :password_approval, comparison: { to: :password, is: :equal_to }
class ComparisonValidator < ActiveModel::EachValidator
OPERATOR_MAPPINGS = {
equal_to: :==,
greater_than: :>,
greater_than_or_equal_to: :>=,
less_than: :<,
@tlux
tlux / jobs.rake
Last active December 16, 2015 14:49
Restarting delayed_job if it has been unexpectedly stopped or crashed using "rake jobs:rescue" in production environment
namespace :jobs do
desc "Restarts delayed_job if it has been unexpectedly stopped or crashed"
task :rescue do
raise "Not in production environment" unless Rails.env.production?
pid_file = "#{Rails.root}/tmp/pids/delayed_job.pid"
restart = false
if File.exist?(pid_file)
pid = File.read(pid_file).strip.to_i
begin
@tlux
tlux / submit_disabler.coffee
Last active December 17, 2015 01:59
Submit Disabler for Rails
$doc = $(document)
$doc.on 'submit', "form:not([data-remote='true'])", ->
$(@).find(':submit').prop('disabled', true)
$doc.on 'ajax:beforeSend', "form[data-remote='true']", ->
$(@).find(':submit').prop('disabled', true)
$doc.on 'ajax:complete', "form[data-remote='true']", ->
$(@).find(':submit').prop('disabled', false)