Skip to content

Instantly share code, notes, and snippets.

View olistik's full-sized avatar

Maurizio De Magnis olistik

View GitHub Profile
require "bundler"
lockfile = Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock"))
dsl = Bundler::Dsl.new
dsl.eval_gemfile("Gemfile")
gems = dsl.dependencies.map(&:name)
lockfile.specs.lazy.select do |spec|
gems.include?(spec.name)
end.map do |spec|
version_parts = spec.version.to_s.split(".")
if version_parts.count > 2
@olistik
olistik / count.rb
Last active October 10, 2015 07:24
files = Dir["files/*.js"]
space_indented_count = files.select do |file|
File.read(file).split("\n").any? do |line|
line.match /^ .*/
end
end.count
puts "Among the #{files.count} most popular Javascript projects in Github, #{space_indented_count} uses space indendation"
(function() {
// here I can declare my private functions
var setup = function() {
var elements = $('[data-toggle=my-feature]');
if (elements.length == 0) {
return;
}
@olistik
olistik / yardoc.shell
Created August 24, 2015 13:29
Yardoc fails to generate documentation for Ruby 2.2.3
ಠ_ಠ wget https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.bz2
ಠ_ಠ tar xvfj ruby-2.2.3.tar.bz2
ಠ_ಠ cd ruby-2.2.3
ಠ_ಠ yardoc --debug *.c
[debug]: Parsing ["addr2line.c", "array.c", "bignum.c", "class.c", "compar.c", "compile.c", "complex.c", "cont.c", "debug.c", "dir.c", "dln.c", "dln_find.c", "dmydln.c", "dmyenc.c", "dmyext.c", "encoding.c", "enum.c", "enumerator.c", "error.c", "eval.c", "eval_error.c", "eval_jump.c", "file.c", "gc.c", "golf_prelude.c", "goruby.c", "hash.c", "id.c", "inits.c", "io.c", "iseq.c", "lex.c", "load.c", "loadpath.c", "localeinit.c", "main.c", "marshal.c", "math.c", "miniinit.c", "miniprelude.c", "newline.c", "node.c", "numeric.c", "object.c", "pack.c", "parse.c", "prelude.c", "proc.c", "process.c", "random.c", "range.c", "rational.c", "re.c", "regcomp.c", "regenc.c", "regerror.c", "regexec.c", "regparse.c", "regsyntax.c", "ruby.c", "safe.c", "signal.c", "siphash.c", "sparc.c", "sprintf.c", "st.c", "strftime.c", "string.c", "struct.c", "symbol.c", "thread.c", "thread_pthread.c"
@olistik
olistik / brave.rb
Last active August 29, 2015 14:26
Be Brave in Ruby-land
module Brave
def self.be(exception_classes = [NoMethodError])
begin
yield
rescue *exception_classes => e
end
end
end
@olistik
olistik / application_controller.rb
Last active August 29, 2015 14:26
JSON dump of params in Rails
class ApplicationController
before_action :log_params
def log_params
# do some stuff with it, for example store it into the database
recursive_backup_params(params: params).to_json
end
def recursive_backup_params(params: {})
@olistik
olistik / Gemfile
Last active August 29, 2015 14:26
Simple wrapper to upload files to S3 in Ruby/Rails
source 'https://rubygems.org'
ruby '2.2.2'
gem 's3', '~> 0.3'
# other stuff
@olistik
olistik / spec_helper.rb
Created July 30, 2015 13:01
Trying to load the db seed only for feature tests.
RSpec.configure do |config|
config.before :suite, type: :feature do
load "#{Rails.root}/db/seeds.rb"
end
end
@olistik
olistik / migrations_shifter.rb
Created July 7, 2015 13:20
Moves Rails migrations by modifying their timestamp
require 'fileutils'
class Object
def pat
if block_given?
yield self
else
self
end
end