Skip to content

Instantly share code, notes, and snippets.

View loz's full-sized avatar

Jonathan Lozinski loz

  • Edinburgh, Glasgow
View GitHub Profile
def counter(start,increment)
start -= increment
lambda do
start += increment
end
end
@loz
loz / gist:907518
Created April 7, 2011 10:36
RubyConf Example Configure Script
class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password, :app_server_config, :port
def app_server
if block_given?
@app_server_config = Configuration.new
yield @app_server_config
else
@app_server_config || Configuration.new
end
@loz
loz / gist:907549
Created April 7, 2011 10:51
Configure Refactored
class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password, :app_server_config, :port
def configurer
if block_given?
@app_server_config = Configuration.new
yield @app_server_config
@app_server_config
else
@app_server_config || Configuration.new
@loz
loz / gist:907811
Created April 7, 2011 13:53
Scottish Ruby Conference - Cheaty 'new' method
class Object
def new(klass, *args)
klass.new(*args)
end
end
x = new String
p x
p new Array
Ruby is awesome!:
if Date.today.wednesday?
else
end
@loz
loz / gist:907868
Created April 7, 2011 14:32
Extend Exercise
module AccessorWrapper
def self.included(base)
base.extend ClassMethods
end
def self.sneaky_logger(klass, accessor)
@logged_stuff ||= {}
@logged_stuff[klass] ||= []
@logged_stuff[klass] << accessor
@loz
loz / gist:907900
Created April 7, 2011 14:44
Extend Example with XTEND not extend
class Object
def self.xtend(klass)
self.class.send(:include, klass)
end
end
module AccessorWrapper
def self.included(base)
#base.extend ClassMethods
File.open('hello.rb', 'wb') {|f| f.write 'class Hello; end' }
x = Module.new
file = File.open('hello.rb') {|f| f.read }
x.module_eval(file)
x::Hello
#Patch Module:
class Module
public
def load_from_file(file)
@loz
loz / gist:2473927
Created April 23, 2012 21:17
Generate gems.tags from your bundle in a project
#!/usr/bin/env ruby
require 'bundler'
require 'bundler/runtime'
puts 'Building gems.tags from bundle'
outpath = 'gems.tags'
begin
runtime = ::Bundler::Runtime.new Dir.pwd, ::Bundler.definition
paths = runtime.specs.map(&:full_gem_path).join(' ').strip
@loz
loz / gist:2940508
Created June 16, 2012 08:24
Setting Session in Sinatra/Rspec
#Took me ages to work this out:
def setup_session(session = {})
Rack::Session::Abstract::SessionHash.stub(:new).and_return(session)
end
#then in your tests
before :each do
setup_session :user_id => User.first.id
get '/user_required'