This file contains hidden or 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
| 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 |
This file contains hidden or 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
| class Fixnum | |
| def even? | |
| (self % 2).zero? | |
| end | |
| def odd? | |
| !even? | |
| end | |
| end |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| class String | |
| def fixnum? | |
| !/^[\d]+$/.match(self).nil? | |
| end | |
| def float? | |
| !/^[\d]+\.[\d]+?$/.match(self).nil? | |
| end | |
| def numeric? |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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) |
This file contains hidden or 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 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' |
This file contains hidden or 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
| # 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: :<, |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| $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) |
OlderNewer