Skip to content

Instantly share code, notes, and snippets.

View clm-a's full-sized avatar

Clément Alexandre clm-a

View GitHub Profile
@clm-a
clm-a / amperstand.rb
Created April 20, 2012 12:05
&method
# From : http://andrewjgrimm.wordpress.com/2011/10/03/in-ruby-method-passes-you/
["1", "2", "3"].map(&method(:Integer))
filenames = ["a.txt", "b.txt", "c.txt"]
texts = filenames.map(&File.method(:read))
@clm-a
clm-a / unicorn.rb
Created April 15, 2012 13:13
Use of unicorn
# Gemfile
gem 'unicorn'
# Procfile
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
# config/unicorn.rb
worker_processes 4
timeout 30
@clm-a
clm-a / hash_checkboxes_rails.rb
Created March 22, 2012 11:48
Checkboxes for rendering an Hash in Rails
# file mood.rb
Mood = Struc.new(:happy)
# file user.rb
class User
serialize mood, Mood
def mood=(attr={})
@clm-a
clm-a / class_samples.rb
Created March 17, 2012 14:03
Some ruby samples playing with classes
# Playing with classes and structs
MyClass = Class.new
p MyClass.class
# => Class
my_instance = MyClass.new
@clm-a
clm-a / root_commit.sh
Created March 10, 2012 10:08
Insert a blank root commit on an existing repo
# from http://stackoverflow.com/a/647451
git symbolic-ref HEAD refs/heads/newroot
git rm --cached -r .
git clean -f -d
# then you apply the same steps
git commit --allow-empty -m 'root commit'
git cherry-pick $(git rev-list --reverse master | head -1)
@clm-a
clm-a / touch_gestures.js
Created August 16, 2011 16:08
Touch gestures
element.addEventListener('touchstart', function(e) {
e.preventDefault();
}, false);
element.addEventListener('touchmove', function(e) {
e.preventDefault();
}, false);
@clm-a
clm-a / join_lines.rb
Created August 16, 2011 16:07
Join multiple lines with a different separator
"toto\ntiti".lines.map{|l| l.strip}.to_a.join(", ")
@clm-a
clm-a / try_hash.rb
Created April 28, 2011 14:02
Try on a nested hash
# http://rubyquicktips.tumblr.com/post/4601358354/using-try-with-a-hash-to-check-existence-of-a-key
hsh = { :existing_key =>
{
:existing_sub_key => :value
}
}
hsh.try(:[], :existing_key).try(:[], :existing_sub_key)
# => :value
@clm-a
clm-a / not_match_whole_word_regexp.pl
Created April 5, 2011 10:39
A regexp not to match a whole word.
# following DOES match "foobar", "fooadmin", "adminbar", "fooadminbar" but not "admin" as a whole word :
/^((?!\badmin\b)[^\s]*)$/
# more reserved keywords :
/^((?!\badmin\b|\bteam\b)[^\s]*)$/
# http://www.regular-expressions.info/wordboundaries.html
# http://www.regular-expressions.info/lookaround.html
@clm-a
clm-a / endless_hash.rb
Created April 1, 2011 14:23
Infine hash
Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }