Skip to content

Instantly share code, notes, and snippets.

View aantix's full-sized avatar

Jim Jones aantix

View GitHub Profile
@smsohan
smsohan / RubyAdvancedFeatures
Created November 12, 2010 05:50
Some advanced ruby features
#the new tap method of ruby will always return itself, but lets you play with!
data = (1..10).to_a
data.tap{|x| print x}.to_a.join(', ')
p
#alias will redirect method calls to method with a different name, useful for api changing
class SomeClass
def new_method(x)
p "The value is #{x}"
@mattwynne
mattwynne / be_same_file_as.rb
Last active May 21, 2022 13:27
RSpec matcher to compare two file, using their MD5 hashes
RSpec::Matchers.define(:be_same_file_as) do |exected_file_path|
match do |actual_file_path|
expect(md5_hash(actual_file_path)).to eq(md5_hash(expected_file_path))
end
def md5_hash(file_path)
Digest::MD5.hexdigest(File.read(file_path))
end
end
@aantix
aantix / seed_model_hash_dump.rb
Created January 1, 2011 02:34
Display model data in hash form for create -- Useful in dumping old Rails model data (for seeds.rb)
model = Object::const_get(ARGV[0])
rows = model.find(:all)
# Any columns you don't want output, list here as symbols
# e.g. BLACKLIST = [:user_id, :workout_id]
BLACKLIST = []
# Any columns that you'd like to rename on the output, list in this hash
# e.g. TRANSFORM = {:percentOfMax => :percent_of_max}
TRANSFORM = {}
@aantix
aantix / factories_from_db.rb
Created January 16, 2011 12:14
Takes a seeded model and dumps out the data in a format that can be used for testing factories
# 523 : ~/Projects/runfatboy2 $ rails runner ~/factories_from_db.rb Exercise 1 2 3
#
# Factory.define :exercise_incline press do |r|
# r.name 'Incline Press'
# r.url 'http://video.google.com/googleplayer.swf?docId=-5762262560903802111&hl=en'
# r.verb 'Lift'
# r.measurement_type_id 3
# r.exercise_type_id 1
# r.user_id 2
# r.start_weight 0.8
@aantix
aantix / sync.rb
Created January 19, 2011 21:38
RSyncs all svn changed files to a remote VM volume. Useful when you want to test changes on a remote VM, but not necessarily commit them to the SVN trunk.
#!/usr/bin/env ruby
ignore_patterns = ["db/","test","log","reports","server","vendor/","Gemfile","tmp",".idea","config/",".DS_Store", ".xml", '.zip']
ignore_statuses = ['!','D','?']
changed_files = (`svn status`).split("\n").collect do |e|
p = e.split(/\s+/)
ignore_statuses.include?(p[0]) ? nil : p[p.size - 1]
end.compact
@dhh
dhh / gist:1014971
Created June 8, 2011 18:09
Use concerns to keep your models manageable
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
@zefer
zefer / heroku_cedar_hirefire_patch.rb
Created July 14, 2011 15:28
Patch HireFire so worker auto-scaling works on the Beta Heroku Cedar stack
# hopefully a temporary patch so HireFire will run on the Heroku Cedar stack
# NB: the worker type is hard-coded as "worker" below, this must correlate to the type in Procfile
# NB: ENV['APP_NAME'] must be defined (e.g. 'heroku config:add APP_NAME=myherokuappname')
# using ps & ps_scale instead of info & set_workers
class HireFire::Environment::Heroku
private
def workers(amount = nil)
@wycats
wycats / marshal_proc.rb
Created November 19, 2011 06:11
Source code for marshaling a Proc in Rubinius. See http://yehudakatz.com/2011/11/19/how-to-marshal-procs-using-rubinius/ for more details
module Rubinius
class CompiledMethod
def _dump(depth)
Marshal.dump([@scope, Rubinius::CompiledFile::Marshal.new.marshal(self)])
end
def self._load(string)
scope, dump = Marshal.load(string)
cm = Rubinius::CompiledFile::Marshal.new.unmarshal(dump)
cm.scope = scope
@burke
burke / 0-readme.md
Created January 27, 2012 13:44 — forked from funny-falcon/cumulative_performance.patch
ruby-1.9.3-p327 cumulative performance patch for rbenv

ruby-1.9.3-p327 cumulative performance patch for rbenv

This installs a patched ruby 1.9.3-p327 with various performance improvements and a backported COW-friendly GC, all courtesy of funny-falcon.

Requirements

You will also need a C Compiler. If you're on Linux, you probably already have one or know how to install one. On OS X, you should install XCode, and brew install autoconf using homebrew.

@jonathanmoore
jonathanmoore / gist:2640302
Created May 8, 2012 23:17
Get the share counts from various APIs

Share Counts

I have always struggled with getting all the various share buttons from Facebook, Twitter, Google Plus, Pinterest, etc to align correctly and to not look like a tacky explosion of buttons. Seeing a number of sites rolling their own share buttons with counts, for example The Next Web I decided to look into the various APIs on how to simply return the share count.

If you want to roll up all of these into a single jQuery plugin check out Sharrre

Many of these API calls and methods are undocumented, so anticipate that they will change in the future. Also, if you are planning on rolling these out across a site I would recommend creating a simple endpoint that periodically caches results from all of the APIs so that you are not overloading the services will requests.

Twitter