Skip to content

Instantly share code, notes, and snippets.

View JacobNinja's full-sized avatar

JR Richardson JacobNinja

View GitHub Profile
urls = ['http://www.google.com', 'http://www.github.com', 'http://www.twitter.com']
concurrent_url_grabber = Enumerator.new do |y|
urls.map do |url|
Thread.new do
result = http_request(url)
# Need a mutex here for multithreaded VMs
y << result
end
end.each(&:join)
# Example of chunking an operation that would use too much memory
# Grit is a git repository library
all_commits = -> grit_repo {
Enumerator.new do |y|
total_commit_count = grit_repo.commit_count
# we can safely handle 500 commits in memory
amount = 500
offset = 0
((total_commit_count / amount) + 1).times do
@JacobNinja
JacobNinja / bugwatch_examples.rb
Last active December 20, 2015 07:09
Bugwatch usage example
repo = Bugwatch::Repo.discover('bugwatch', Dir.pwd)
# => #<Bugwatch::Repo:0x00000101c98ae0 @name="bugwatch", @url="/Users/jacobr/code/bugwatch">
commit = repo.commits.first
# => #<Bugwatch::Commit:0x00000101ca0100 @attributes={:sha=>"d7ecd8b082906df27be6d973a196f32621d5ad1f", :message=>"add the rest of the headers", :diffs=>#<Enumerator: #<Enumerator::Generator:0x00000101ca0218>:each>, :stats=>#<Enumerator: #<Enumerator::Generator:0x00000101ca0290>:each>, :tree=>#<Bugwatch::Tree:0x00000101ca01c8 @grit=#<Grit::Tree "658dc44b7abec270b90121798e111202ac040dd9">>, :author_name=>"Jacob Richardson", :author_email=>"jacobr@groupon.com", :authored_date=>2013-07-23 12:22:49 -0600, :committer_name=>"Jacob Richardson", :committer_email=>"jacobr@groupon.com", :committed_date=>2013-07-23 12:22:49 -0600}>
commit.author_name
# => "Jacob Richardson"
commit.authored_date
# => 2013-07-23 12:22:49 -0600
commit.files
# => ["Gemfile", "Rakefile", "bugwatch.gemspec", "features/exceptions/commit_finder.feature", "features/step_defin
(defn unsafe-deserialization? [node]
(and (call-with-receiver-node? node)
(eq-receiver? node "YAML")
(some #(eq-method-name? node %)
["load" "load_documents" "load_stream" "parse_documents" "parse_stream"])))
@JacobNinja
JacobNinja / gist:a38d0f4b02759c910dc3
Created May 2, 2014 16:03
Mandatory keyword args
2.1.1 :001 > def test(foo:)
2.1.1 :002?> puts foo
2.1.1 :003?> end
=> :test
2.1.1 :004 > test()
ArgumentError: missing keyword: foo
jacob@Ninja:$ bin/benchmark_rails
Rehearsal ------------------------------------
"Codeminer encountered 2 errors parsing 1711 files"
6.520000 0.040000 6.560000 ( 6.564404)
"RubyParser encountered 26 errors parsing 1711 files"
17.110000 0.240000 17.350000 ( 17.340777)
-------------------------- total: 23.910000sec
user system total real
class RenameTableToLongerName < ActiveRecord::Migration
def change
remove_index :table, name: 'index_table_on_something_id'
remove_index :table, name: 'index_table_on_something_id'
rename_table :table, :incredibly_long_table_name_overflowing_index_length
add_index :incredibly_long_table_name_overflowing_index_length, :something_id, name: 'index_iltnoil_on_something_id'
add_index :incredibly_long_table_name_overflowing_index_length, :something_id, name: 'index_iltnoil_on_something_id'
end
end
@JacobNinja
JacobNinja / patterns.rb
Created June 27, 2014 18:40
Ruby anti-patterns
# For loop
# suggested: replace with each
for i in x
end
# each_with_object mutates array
# suggested: replace with map
each_with_object([]) |list, i|
list << i
end
@JacobNinja
JacobNinja / substitution_example.rb
Created July 9, 2014 21:45
Source tree substitution examples
# Example 1: Swap the consequence and else statements in an if expression
if_node = CodeMiner.parse(<<-RUBY).each.first
if foo
begin
do_something
end
else
do_something_else
end
Parse results for Rails
user system total real
"Codeminer parse results: 0 parse errors, 0 runtime errors, 1621 files total"
7.760000 0.080000 7.840000 ( 7.980644)
"Codeminer sexp results: 0 parse errors, 0 runtime errors, 1621 files total"
11.080000 0.040000 11.120000 ( 11.136238)
"Codeminer (RubyParser compatible) sexp results: 0 parse errors, 0 runtime errors, 1621 files total"
11.250000 0.070000 11.320000 ( 11.317630)
"Codeminer flog results: 0 parse errors, 0 runtime errors, 1621 files total"