Skip to content

Instantly share code, notes, and snippets.

View kisai's full-sized avatar

Sergio Diaz kisai

  • UNOSQUARE
  • Mexico
View GitHub Profile
@kisai
kisai / movedir.clj
Created May 6, 2012 08:20
Move directory contents from source to destination
(use 'clojure.java.io)
(let [pred (every-pred
#(.isDirectory %)
#(not (.startsWith (.getName %) "."))
#(not (some (partial = (.getName %)) ["Exclude Name 0" "Exclude Name 1" "Exclude Name 2"])))
dest "/Path/to/Destination"
src "/Path/to/Source" ]
(doseq [f (filter pred (-> src file .listFiles seq))]
(.renameTo f (file dest (.getName f))) ))
(define (memoize f)
(define ht (make-hash))
(define (f-mem . args)
(if (hash-has-key? ht args)
(hash-ref ht args)
(begin
(hash-set! ht args (apply f args))
(hash-ref ht args))))
f-mem)
#!/bin/bash
export JAVA_OPTS="-Xmx2048m -XX:ReservedCodeCacheSize=256m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -XX:NewRatio=3 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSClassUnloadingEnabled -Dfile.encoding=UTF-8 -XX:+TieredCompilation -XX:TieredStopAtLevel=1"
export JRUBY_OPTS="--1.9 -J-noverify -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1"
torquebox run -b 0.0.0.0
@kisai
kisai / ar_build_method_sample.rb
Last active August 29, 2015 14:05
activerecord :build method
Class Dog
has_many :tags
belongs_to :person
end
Class Person
has_many :dogs
end
d = Dog.new
@kisai
kisai / format_ruby_exception.rb
Last active August 29, 2015 14:05
How to format ruby exception with backtrace into a string
#Sample 1
puts "#{$@.first}: #{$!.message} (#{$!.class})", $@.drop(1).map{|s| "\t#{s}"}
#Sample 2
puts "#{e.backtrace.first}: #{e.message} (#{e.class})", e.backtrace.drop(1).map{|s| "\t#{s}"}
@kisai
kisai / precedence_nightmare.rb
Last active August 29, 2015 14:05
precedence rule nightmare.... damn i love lisp (pun made on a comment "damn i love ruby" when he was using zippers an FP algorithm)
true || true and false #=> false
true || true && false #=> true
@kisai
kisai / find_duplicate_array.rb
Created August 20, 2014 16:10
Find duplicates in a array
ary = ["A", "B", "C", "B", "A"]
ary.group_by { |e| e }.select { |k, v| v.size > 1 }.map(&:first)
ary.sort.chunk { |e| e }.select { |e, count| count.size > 1 }.map(&:first)
ary.select { |e| ary.count(e) > 1 }.uniq
rails g migration add_public_and_private_to_document public:string private:string
bin/rails generate migration AddSomeRefAndSomeColumnToSomeTable some:references some_column:boolean
@kisai
kisai / add_method_to_object.rb
Last active August 29, 2015 14:05
Meat: Add method to an instanced object
obj = SomeObject.new
obj.define_singleton_method(:new_method) do
"do some things"
end
# Using a mixin
module AdditionalMethods
def new_method
@kisai
kisai / my_attr_accessor.rb
Created August 23, 2014 05:44
My attr_accesor.rb
class Class
#firstly, the * decoration on the parameter variable
#indicates that the parameters should come in as an array
#of whatever was sent
def my_attr_accessor(*args)
#We simply iterate through each passed in argument...
args.each do |arg|
#Here's the getter