Skip to content

Instantly share code, notes, and snippets.

@rahult
rahult / Main.purs
Last active Aug 11, 2017
Complex Number
View Main.purs
module Main where
import Prelude
newtype Complex = Complex {
real :: Number,
imaginary :: Number
}
instance showComplex :: Show Complex where
View gist:fce1ddf795e911211f9a
Verifying I am +rahult on my passcard. https://onename.com/rahult
View README.md

Let's say you have a model, with an files attached, using Paperclip. You have a couple millions of those files and you're not sure that every one of them (and all its thumbnails) are still used by a database record.

You could use this rake task to recursively scan all the directories and check if the files need to be kept or destroyed.

In this example, the model is called Picture, the attachment is image and the path is partitioned like images/001/412/497/actual_file.jpg

The task is going down the path. Each time the path ends with 3 triplets of digits ("001/412/497" for example) it looks for a record with the ID 1412497. If such a record doesn't exist, the whole directory is moved to a parallel images_deleted directory. At the end you can delete the files if you like, or move them to an archive location.

You can use the "dry run" mode : to print which files would be removed

View ams_cache.rb
class User < ActiveRecord::Base; end;
class Post < ActiveRecord::Base; end;
class Comment < ActiveRecord::Base; end;
class AuthorSerializer < ActiveModel::Serializer
attributes :id, :name :created_at, :updated_at
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :body :created_at, :updated_at
View keybase.md

Keybase proof

I hereby claim:

  • I am rahult on github.
  • I am rahult (https://keybase.io/rahult) on keybase.
  • I have a public key whose fingerprint is 5936 554B 0BE1 0732 6CD4 EA1F E963 03C9 1B4E AA98

To claim this, I am signing this object:

@rahult
rahult / parameterize.rb
Created Apr 22, 2014
Parameterize text
View parameterize.rb
#!/usr/bin/ruby
def parameterize(string, sep = '-')
# Turn unwanted chars into the separator
string.gsub!(/[^a-z0-9\-_]+/i, sep)
unless sep.nil? || sep.empty?
re_sep = Regexp.escape(sep)
# No more than one of the separator in a row.
string.gsub!(/#{re_sep}{2,}/, sep)
# Remove leading/trailing separator.
View config.fish
### Will save you're ass! ###
# Set C compiler
set -gx CC gcc
# Set architecture flags
set -gx ARCHFLAGS "-arch x86_64"
# fish functions and aliases
alias ls "ls -GF"
# fish shell greeting
@rahult
rahult / credit_card_validator_spec.rb
Created Jul 29, 2012
Offline Credit Card Validator Specs
View credit_card_validator_spec.rb
# Ruby Version: ruby-1.9.2-p290
# Author: Rahul Trikha
# Email: rahul.trikha@gmail.com
# Run: ruby credit_card_validator_spec.rb
# Include the latest version of minitest
# Must and won't predicates do not work with the
# shipped version of minitest with ruby
# Please install the latest minitest gem before running the specs
# $ gem install minitest -v=3.3.0
@rahult
rahult / credit_card_validator.rb
Created Jul 29, 2012
Offline Credit Card Validation Script
View credit_card_validator.rb
# Ruby Version: ruby-1.9.2-p290
# Author: Rahul Trikha
# Email: rahul.trikha@gmail.com
# Run: ruby credit_card_validator.rb
class CreditCardValidator
attr_accessor :card
def initialize(card=nil)
sanitize(card)
@rahult
rahult / fakeout.rake
Created Jan 17, 2012 — forked from matthutchinson/fakeout.rake
fakeout.rake - a simple/configurable rake task that generates some random fake data for the app (using faker) at various sizes
View fakeout.rake
# a simple/configurable rake task that generates some random fake data for the app (using faker) at various sizes
# NOTE: requires the faker or ffaker gem
# sudo gem install faker - http://faker.rubyforge.org
# OR
# sudo gem install ffaker - http://github.com/EmmanuelOga/ffaker
require 'faker'
class Fakeout