Skip to content

Instantly share code, notes, and snippets.

View davingee's full-sized avatar

Scott Smith davingee

View GitHub Profile
require "formula"
class BrowsermobProxy < Formula
homepage "http://bmp.lightbody.net/"
url "https://github.com/lightbody/browsermob-proxy/releases/download/browsermob-proxy-2.1.4/browsermob-proxy-2.1.4-bin.zip", :using => :curl
sha1 "27c4080411adff919586e909c664c73bebb8ba8bfcaea259ce58327222e5e8fb"
version "2.1.4"
def install
prefix.install Dir["*"]
end
module ActiveRecord
module Type
class Json < ActiveModel::Type::Value
include ActiveModel::Type::Helpers::Mutable
def deserialize(value)
value = '{}' if value.is_a?(::NilClass)
return value unless value.is_a?(::String)
ActiveSupport::JSON.decode(value) rescue nil
end
end
@davingee
davingee / flatten_ints
Last active May 30, 2018 18:52
This is an Array instance method that flattens any kind of array or array of arrays that contain integers (Fixnum)
require 'rspec'
class Array
def flatten_ints(all=[])
self.each do |item|
if item.class == Array
item.flatten_ints(all)
else
unless item.class == Fixnum
raise TypeError.new "This Methos is only for flattening Fixnums"
require 'pry'
require 'benchmark'
require 'benchmark-memory'
class Array
def top_occurrence_0(k)
counter = Hash.new{ |k, v| k[ v ] = 0 }
self.each { |id| counter[ id ] += 1 }
counter.sort_by{ |id, counts| -counts }[0, k].map{ |id_count| id_count[0] }
require 'pry'
require 'benchmark'
require 'benchmark-memory'
class User
def initialize( args = {} )
raise 'user_ids needs to be an array' unless args[:user_ids].is_a?(Array)
@user_ids = args[:user_ids]
end
# You are being asked to implement classes to manage a tree of nodes. This should be done in Ruby.
class TreeNode
attr_accessor :value, :key, :children
def initialize( args = {} )
self.value = args.fetch(:value, 1)
self.key = args.fetch(:key, 'abandoned')
self.children = args.fetch(:children, {})
end
# You are being asked to implement classes to manage a tree of nodes. This should be done in Ruby.
class TreeNode
attr_accessor :value, :key, :children
def initialize( args = {} )
self.value = args.fetch(:value, 1)
self.key = args.fetch(:key, 'abandoned')
self.children = args.fetch(:children, {})
end
module Boolean
end
TrueClass.send :include, Boolean
FalseClass.send :include, Boolean
true.is_a?(Boolean) #=> true
module ArrayExtensions
def rpermute( possibilities = self.uniq.count, p_array = [], index = 0 )
0.upto( possibilities - 1 ) do |i|
require 'active_support/core_ext/enumerable'
require 'pry'
class ProjectEuler
attr_accessor :hash, :array
def initialize( args = {} )
self.hash = {}
self.array = []
end