Skip to content

Instantly share code, notes, and snippets.

Avatar

Chris Bielinski chrisb

View GitHub Profile
@chrisb
chrisb / hash_extension.rb
Created Dec 14, 2010
Hash extension for safe access to possibly undefined elements
View hash_extension.rb
class Hash
def value_at(*keys)
begin
memo = self.dup
keys.each { |key| memo = memo[key] }
memo
rescue
nil
end
end
View rapper.rb
class Rapper
attr_accessor :name
def initialize(n)
self.name = n
end
def rap(words)
puts "Yo yo yo, my name is #{self.name}"
puts words
end
end
View gist:1299837
def current_language
# if a "lang" URL parameter is specified, force it
return params[:lang] if params[:lang].present?
# if the user is logged in, use their language preference stored in the DB
return current_user.language if logged_in?
# if the user has manually selected a language, return that
return cookies[:language_code] if cookies[:language_code]
View gist:1714454
require 'sinatra'
require 'rubygems'
require 'open-uri'
require 'nokogiri'
get '/' do
doc = Nokogiri::HTML(open("http://www.google.com"))
balls = []
doc.css('a').each do |node|
View gist:2026670
<html>
<head>
<title>my title</title>
</head>
<body>
<script>
document.body.style.background = 'lightgreen';
document.write("<div style='font-family:Arial,sans-serif;font-size:14pt;font-weight:bold;'>Hello, world! Isn't life great?</div>");
</script>
<noscript>
@chrisb
chrisb / delayed_job.rb
Created Apr 25, 2012
Log DJ ActiveRecord activity to a separate log file
View delayed_job.rb
Delayed::Worker.logger = ActiveSupport::BufferedLogger.new("log/#{Rails.env}_delayed_jobs.log", Rails.logger.level)
Delayed::Worker.logger.auto_flushing = 1
if caller.last =~ /.*\/script\/delayed_job:\d+$/
Rails.logger = Delayed::Worker.logger
ActiveRecord::Base.logger = Delayed::Worker.logger
end
View gist:3668627
class Foo
def hello
do_something_really_destructive!
end
protected
def bar
do_something_really_destructive!
View gist:3960866

the Sun

"first is fleet of foot/ the second believes in love/ the third is blue and the fourth is red/ when looked on from above/ the fifth is King presiding/ the sixth and seventh his fathers/ the eighth his crowned brother/ circling with the others/ who do they worship?"

the law

"it cannot be held / but it can be broken / its eyes are everywhere / no one is above it / but not everyone is behind it / ..."

secret

"riddle me this: / what is it that you can hold and keep, / but disappears as soon as you / pass it to another?"

pi

View gist:4172555
function wysiwymTslt(strKey) {
var locale = $('html').attr('lang');
return {
'bold': { en: 'Bold', fr: '' },
'italic': { en: 'Italic', fr: '' },
'italics': { en: 'italics', fr: '' },
'link': { en: 'Link', fr: '' },
'blockquote': { en: 'Blockquote', fr: '' },
'large_code_block': { en: 'Large Code Block', fr: '' },
'quote': { en: 'Quote', fr: '' },
View gist:4583193
# untained_csv_each(File.open('my.csv'),'Date') do |row|
# # do something with row
# end
def untainted_csv_each(file,line_match,&block)
tmp_file = File.open(file.path+"-tmp","w+")
garbage_passed = false
file.each do |line|
garbage_passed = true if line =~ /^"(.*)#{line_match}"/ && !garbage_passed