Skip to content

Instantly share code, notes, and snippets.

@madwork
madwork / .pryrc
Last active December 11, 2015 01:28
Mac OS X 10.8 : RVM + Homebrew + Ruby 1.9.3 + .pryrc
require 'benchmark'
# textmate as editor
Pry.config.editor = 'mate -w'
Pry.config.color = true
Pry.config.pager = true
# alias 'q' for 'exit'
Pry.config.commands.alias_command 'q', 'exit-all'
@madwork
madwork / contact.rb
Last active December 12, 2015 06:38
nested attributes with dynamic form
class Contact < ActiveRecord::Base
has_many :responses
has_many :dynaforms, through: :responses
attr_accessible :email, :responses_attributes
accepts_nested_attributes_for :responses
def self.new_with_dynaforms
new.tap do |contact|
@madwork
madwork / exporter.rb
Last active December 14, 2015 01:18
Simple CSV Exporter
require 'iconv'
class Exporter
def initialize(separator = ';')
@separator = separator
@header = []
@body = []
end
@madwork
madwork / README.md
Last active December 14, 2015 15:48
Various method to add dynamic accessors (getters/setters)

Various method to add dynamic accessors (getters/setters)

Make it yourself way

foo = Foo1.new
foo.build1 :a, :b
foo.a = 1
foo.a

foo = Foo1.new

@madwork
madwork / bench.rb
Created March 12, 2013 18:29
String prepend FTW
require 'benchmark'
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("String::prepend") { "foo".prepend "tmp/" }
x.report("File.join") { File.join "tmp", "foo" }
end
puts
Benchmark.bm do |x|
x.report("String::prepend") { 1000000.times{"foo".prepend "tmp/"} }
@madwork
madwork / install.sh
Last active December 14, 2015 21:58
Max OS X 10.8 + chruby + Ruby 2.0
brew install chruby readline openssl libyaml
brew link openssl --force
export ARCHFLAGS="-arch x86_64"
export CFLAGS="-g -O2"
export LDFLAGS="-L/usr/local/lib -L/usr/local/opt/readline/lib"
export CPPFLAGS="-I/usr/local/include -I/usr/local/opt/readline/include"
mkdir ~/.rubies
wget http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p353.tar.gz
@madwork
madwork / cipher.rb
Created April 17, 2013 16:24
Cipher implementation
#!/usr/local/bin/ruby
#
# http://www.ruby-doc.org/stdlib-2.0/libdoc/openssl/rdoc/OpenSSL/Cipher.html
require 'base64'
require 'openssl'
# create the cipher for encrypting
cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
cipher.encrypt
@madwork
madwork / gemrc
Created May 14, 2013 15:41
rubygems configuration file
---
:verbose: true
gem: --no-ri --no-rdoc --prerelease
:update_sources: true
:sources:
- https://rubygems.org
:backtrace: false
:bulk_threshold: 1000
:benchmark: false
@madwork
madwork / powrc
Created June 9, 2013 10:55
powrc with chruby
export SSL_CERT_FILE=/usr/local/share/ca-bundle.crt
source /usr/local/share/chruby/chruby.sh
chruby $(cat .ruby-version)
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8