Skip to content

Instantly share code, notes, and snippets.

View thomasklemm's full-sized avatar

Thomas Klemm thomasklemm

View GitHub Profile
@thomasklemm
thomasklemm / code_generator.rb
Created September 12, 2012 09:05
Code Generator
LETTERS = ('a'..'z').to_a
NUMBERS = (0..9).to_a
LETTERS_AND_NUMBERS = LETTERS.concat(NUMBERS)
# Generate a random unique ID
# in any length
class CodeGenerator
def generate(n=16)
code = []
n.times {code << LETTERS_AND_NUMBERS.sample}
@thomasklemm
thomasklemm / speaking_timer.rb
Created September 15, 2012 16:40
Speaking Timer from my training
# encoding: utf-8
require 'talks'
class Hash
##
# Cascading Fetch
#
# Tries to fetch provided symbols one after the other
# and return fallback non-symbol value otherwise
@thomasklemm
thomasklemm / array_of_arrays_to_hash.rb
Created September 15, 2012 17:07
Array of arrays to hash conversion
##
# Array of arrays to hash conversion
#
##
# Version 1
array = [[1, 1], [2, 2], [3, 3]]
hash = Hash[*array.flatten]
@thomasklemm
thomasklemm / sma9.rb
Created October 30, 2012 11:03
Calculation of Simple Moving Average 9
# Require the json library to parse json into Ruby objects
require 'json'
require 'simple_statistics'
# The input
json = '[
{
"Low": 8.63,
"Volume": 14211900,
"Date": "2012-10-26",
@thomasklemm
thomasklemm / range_operations.rb
Created October 30, 2012 14:01
Operations with Ranges
class Range
def end_at(x)
Range.new(self.begin, x)
end
def start_at(x)
Range.new(x, self.end)
end
end
@thomasklemm
thomasklemm / toc_ruby.rb
Created December 5, 2012 20:27
Table of Contents - Structured Layout in Ruby
##
# A ruby-powered table of contents layout
words = %w(we are the world lets make it a better place we are the world)
words.each_with_index {|word, page| puts word.ljust(10, '.') + (page*page).to_s.rjust(3, '.')}
# => Output
# we..........0
# are.........1
@thomasklemm
thomasklemm / formtastic-custom.sass
Created January 4, 2013 12:25
Formtastic Custom - Base Stylesheet transformed to SASS
// Formtastic stylesheet based on the gem's version, retrieved 2013-01-04
// This version reflects the gem exactly.
// Use as a base for modifications.
.formtastic
margin: 0
padding: 0
// resets
ul, ol, li, fieldset, legend, input, button, textarea, select, p
@thomasklemm
thomasklemm / devise.de.yml
Created October 4, 2013 06:14
Devise 3.1.0 i18n German
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n
de:
devise:
confirmations:
confirmed: "Vielen Dank für Deine Registrierung. Bitte melde dich jetzt an."
confirmed_and_signed_in: "Vielen Dank für Deine Registrierung. Du bist jetzt angemeldet."
send_instructions: "Du erhältst in wenigen Minuten eine E-Mail, mit der Du Deine Registrierung bestätigen kannst."
send_paranoid_instructions: "Falls Deine E-Mail-Adresse in unserer Datenbank existiert erhältst Du in wenigen Minuten eine E-Mail mit der Du Deine Registrierung bestätigen kannst."
failure:
@thomasklemm
thomasklemm / bootstrap-forms.sass
Created October 5, 2013 14:08
Style a basic Rails form with Twitter Bootstrap 3.0
form
.field
@extend .form-group
input[type=text], textarea, input[type=email], input[type=search], input[type=password]
@extend .form-control
label
@extend .control-label
.field .field_with_errors
@extend .has-error
@thomasklemm
thomasklemm / calendar_week.rb
Created October 29, 2013 22:02
Calendar week calculations
require 'active_support/core_ext'
def calendar_week(week)
now = Date.current
year = now.cweek < week ? now.year : now.year + 1
Date.commercial(year, week, 1)
end
p calendar_week(49)
# => Mon, 02 Dec 2013