Skip to content

Instantly share code, notes, and snippets.

View chrisb's full-sized avatar

Chris Bielinski chrisb

View GitHub Profile
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|
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]
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
@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 MyController < ApplicationController
helper_method :do_something
# ...
protected
def do_something
# ...
end
@chrisb
chrisb / fizzbuzz.rb
Last active August 29, 2015 13:56
FizzBuzz
(1..100).each { |i|s='';s+='Fizz'if i%3==0;s+='Buzz'if i%5==0;puts s==''?i:s }