Skip to content

Instantly share code, notes, and snippets.

View chrisb's full-sized avatar
👋
Oh haiiii

Chris Bielinski chrisb

👋
Oh haiiii
View GitHub Profile
@chrisb
chrisb / hash_extension.rb
Created December 14, 2010 23:13
Hash extension for safe access to possibly undefined elements
class Hash
def value_at(*keys)
begin
memo = self.dup
keys.each { |key| memo = memo[key] }
memo
rescue
nil
end
end
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
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]
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|
<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 April 25, 2012 01:00
Log DJ ActiveRecord activity to a separate log file
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
class Foo
def hello
do_something_really_destructive!
end
protected
def bar
do_something_really_destructive!

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

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: '' },
# 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