Skip to content

Instantly share code, notes, and snippets.

View LeFnord's full-sized avatar
🚀

peter scholz LeFnord

🚀
  • Leipzig/Lausitz, Saxony
View GitHub Profile
# stolen from http://github.com/cschneid/irclogger/blob/master/lib/partials.rb
# and made a lot more robust by me
# this implementation uses erb by default. if you want to use any other template mechanism
# then replace `erb` on line 13 and line 17 with `haml` or whatever
module Sinatra::Partials
def partial(template, *args)
template_array = template.to_s.split('/')
template = template_array[0..-2].join('/') + "/_#{template_array[-1]}"
options = args.last.is_a?(Hash) ? args.pop : {}
options.merge!(:layout => false)
@henrik
henrik / hash_deep_diff.rb
Created July 14, 2009 08:38
Recursively diff two Ruby hashes.
# Recursively diff two hashes, showing only the differing values.
# By Henrik Nyh <http://henrik.nyh.se> 2009-07-14 under the MIT license.
#
# Example:
#
# a = {
# "same" => "same",
# "diff" => "a",
# "only a" => "a",
# "nest" => {
@isaacs
isaacs / node-and-npm-in-30-seconds.sh
Last active May 16, 2024 16:51
Use one of these techniques to install node and npm without having to sudo. Discussed in more detail at http://joyeur.com/2010/12/10/installing-node-and-npm/ Note: npm >=0.3 is *safer* when using sudo.
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl https://www.npmjs.org/install.sh | sh
@LeFnord
LeFnord / rails3_template.rb
Created November 12, 2010 18:24
An application template for Rails 3 including: authlogic, haml, rspec, compass-susy, jquery ... go running ;)
# An application template for Rails 3
gem "factory_girl_rails", :group => [:development, :test]
gem "factory_girl_generator", :group => [:development, :test]
gem "rspec", :group => [:development, :test]
gem "rspec-rails", :group => [:development, :test]
gem "haml"
gem "haml-rails"
gem "authlogic", :git => "git://github.com/odorcicd/authlogic.git", :branch => "rails3"
@rkh
rkh / chat.rb
Created December 14, 2011 12:55
Simple Chat Application using the Sinatra Streaming API
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do
@rmw
rmw / hash.rb
Created May 16, 2012 13:50
Extend Ruby Hash with method to return a recursive OpenStruct
class Hash
# options:
# :exclude => [keys] - keys need to be symbols
def to_ostruct_recursive(options = {})
convert_to_ostruct_recursive(self, options)
end
private
def convert_to_ostruct_recursive(obj, options)
result = obj
@datenimperator
datenimperator / Gemfile
Created September 7, 2012 18:55
Sinatra, sprockets, compass, bootstrap-sass playing together
source :rubygems
gem 'shotgun', :group=>:development
gem 'rack-cache'
gem 'sinatra', :require => 'sinatra/base'
gem 'sinatra-support'
gem 'haml'
@knugie
knugie / bit_count_benchmark.rb
Last active May 25, 2022 19:25
bit_count_benchmark
#!/usr/bin/env ruby
# coding: utf-8
require 'benchmark'
@count = 2_000_000
@lenght = 49
@pre_compute_16 = Array.new((2**16)) { |f| f.to_s(2).count('1') }
Strategy = Struct.new(:name, :method, :before)
@chrismytton
chrismytton / README.md
Last active December 14, 2015 05:19
Literate Ruby

Literate Ruby

Inspired by Literate CoffeeScript.

$ cat hello.rb.md
Here's a simple program

    puts "Hello, world"
$ ruby litrb.rb < hello.rb.md

Hello, world

@glucero
glucero / ruby_archive.rb
Last active December 20, 2015 17:09
self extracting ruby archiver
#!/usr/bin/env ruby
class RubyArchive
require 'rubygems/package'
require 'zlib'
attr_reader :path
class Archive