Skip to content

Instantly share code, notes, and snippets.

# Mixin to scan an array for objects that respond to a given method
module WithMethod
# Iterates through the array and returns a list of items that
# respond to method
def with_method(method)
select { |item| item.respond_to? method }
end
end
def add(num1)
lambda { |num2| num1 + num2 }
end
add(2).call(2) # returns 4
require 'rubygems'
require 'hpricot'
doc = Hpricot( File.open("file.html") )
(doc / "span.foo").each do |span|
span.swap("***#{span.inner_html}***")
end
# Create a new hash and extend it with our CollectKeyValue
# module
hash ={
"key_1"=>"one",
"key_2"=>"two",
"key_3"=>"three",
"key_4"=>"four"
}.extend(CollectKeyValue)
# Now lets call our new hash#collect_kv method and rename
# Module containing a collect_kv method to allow
# a new hash to be generated my collect instead
# of returning an array of modified keys or values
module CollectKeyValue
# Takes a block that returns a [key, value] pair
# and builds a new hash based on those pairs
def collect_kv
result = {}
each do |k,v|
@snuxoll
snuxoll / init.rb
Created January 30, 2009 11:49
Configuration example for SafeSession
Merb::Config.use do |c|
c[:use_mutex] = false
# Setup the session store, require the safe_session extension to
# generate a unique salt for the application if one does not
# already exist
c[:session_store] = 'cookie'
require File.join(Merb.root, "merb/extensions/safe_session.rb")
safe_session = SafeSession.new
c[:session_secret_key] = safe_session.salt
c[:session_id_key] = '_irsea_session_id'
@snuxoll
snuxoll / safe_session.rb
Created January 30, 2009 11:36
Safe session extension for merb
# Safe session extension for merb applications. Generates a
# unique session salt for an application install automatically
# instead of requiring the user to do so themselves.
class SafeSession
require "digest/sha1"
attr_reader :salt
# Attempts to load the session salt from the saltfile, if it does
# not exist a new salt is generated