Skip to content

Instantly share code, notes, and snippets.

View alainravet's full-sized avatar

Alain Ravet alainravet

  • Braine l'Alleud, Belgium
View GitHub Profile
module DefaultValues
def has_default_values(default_values = {})
cattr_accessor :default_values
self.default_values = default_values
after_initialize :assign_default_values
include InstanceMethods
@markmeeus
markmeeus / gist:6412088
Last active August 31, 2017 12:28
A patch to reuse Moped connections in Mongoid inside Celluliod Actors. If you use Sidekiq with loads of work, you may want reuse your connections. Do not use this toghether with kiqstand as they try to achieve opposite results.
module Celluloid
class Thread < ::Thread
def []= key, value
if key_on_thread? key
thread_vars[key] = value
else
super
end
end
def required(arg=nil)
method = caller_locations(1,1)[0].label
raise ArgumentError.new("required parameter #{arg.to_sym.inspect + ' ' if arg}not passed to method #{method}")
end
def say(greeting: required('greeting'))
puts greeting
end
def say2(greeting: required)
@sstephenson
sstephenson / Simple Encryption.md
Created April 11, 2013 23:48
Simple file/stream encryption using OpenSSL

Simple file/stream encryption using OpenSSL

Create and store a 512-byte random encryption key named secret:

$ mkkey secret

Encrypt the contents of file with the secret key and write it to file.enc:

$ encrypt secret < file > file.enc
@alainravet
alainravet / binary_file_string_utils.rb
Last active December 14, 2015 04:49
extract POIs from a TomTom *.ov2 file (in Ruby)
module BinaryFileStringUtils
def byte_to_int
self.unpack('U').first
end
def little_endian_to_int
# source : http://stackoverflow.com/questions/5236059/unpack-signed-little-endian-in-ruby
arr, bits, num = self.unpack('V*'), 0, 0
arr.each do |int|
num += int << bits
@michaelfairley
michaelfairley / immutable-ruby.md
Last active October 8, 2019 14:15
Immutable Ruby

Immutable Ruby

Libraries I talked about

  • ice_nine: Deep freeze ruby objects
  • Values: Simple immutable value objects for ruby
  • immutable_attributes: specify attributes within an ActiveRecord model that can be set but not modified
  • hamster: Efficient, Immutable, Thread-Safe Collection classes for Ruby

Next Steps

anonymous
anonymous / concerning.rb
Created December 18, 2012 18:00
class Module
# We often find ourselves with a medium-sized chunk of behavior that we'd
# like to extract, but only mix in to a single class.
#
# We typically choose to leave the implementation directly in the class,
# perhaps with a comment, because the mental and visual overhead of defining
# a module, making it a Concern, and including it is just too great.
#
#
# Using comments as lightweight modularity:
@clarkware
clarkware / lyrics.rb
Created September 10, 2012 20:05
Random Lyrics
#!/usr/bin/env ruby
# API Documentation at http://api.wikia.com/wiki/LyricWiki_API
require 'open-uri'
require 'json'
require 'tmpdir'
ARTIST = "Johnny Cash"
@piscisaureus
piscisaureus / pr.md
Created August 13, 2012 16:12
Checkout github pull requests locally

Locate the section for your github remote in the .git/config file. It looks like this:

[remote "origin"]
	fetch = +refs/heads/*:refs/remotes/origin/*
	url = git@github.com:joyent/node.git

Now add the line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to this section. Obviously, change the github url to match your project's URL. It ends up looking like this:

@JEG2
JEG2 / tweet_oge_price_signal.rb
Created August 1, 2012 19:20
A simple bot for tweeting OGE's daily price signal
require "rest_client"
require "twitter"
URL = "http://www.oge.com/residential-customers/products-and-services/" +
"Positive-Energy-Smart-Grid/Pages/PriceSignal.aspx"
DATE_RE = Time.now.strftime("%A,\\s+%B\\s+%d,\\s+%Y")
unless ARGV.size == 4
abort "USAGE: #{$PROGRAM_NAME} CONSUMER_KEY CONSUMER_SECRET " +
"OAUTH_TOKEN OAUTH_TOKEN_SECRET"