This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Article < ActiveRecord::Base | |
xapit do |index| | |
index.text :description do |desc| | |
desc.split.map do |word| | |
if word.include? '-' | |
[word, word.gsub('-', ''), word.gsub('-', ' ')] | |
else | |
word | |
end | |
end.flatten |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# COMMUNITY CHALLENGE | |
# | |
# How would you test this Quiz#problem method? Only two rules: | |
# | |
# 1. The tests should fail if any part of the application breaks. | |
# For example: If "gets" is moved before "puts" then the tests should | |
# fail since that breaks the application. | |
# | |
# 2. You cannot change the Quiz class. But you can use whatever framework | |
# and tools you want for the tests. (RSpec, Cucumber, etc.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MockIO | |
def initialize(receiver = nil) | |
@buffer = "" | |
@receiver = receiver || MockIO.new(self) | |
end | |
def gets | |
1000.times do |n| | |
sleep 0.01 if n > 800 # throttle if it seems to be taking a while | |
unless @buffer.empty? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mimic Mac OS X Finder's sort by name. | |
class Array | |
def finder_sort | |
sort_by do |str| | |
punctuation = %w[` ^ _ - , ; ! ? ' " ( ) [ ] { } @ *] + ['\\'] + %w[& # % + < = > | ~ $] | |
str.to_s.gsub(/\d+|[^a-z\s]/i) do |match| | |
if punctuation.include? match | |
"1".rjust(200 - punctuation.index(match), "0") | |
elsif match =~ /^0+$/ # make an exception for zeros | |
"1".rjust(101, "0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
orders = {} | |
all_orders.group_by(&:ItemCode).each do |item_code, code_orders| | |
orders[item_code] = {} | |
code_orders.group_by(&:year).each do |year, year_orders| | |
orders[item_code][year] = {} | |
year_orders.group_by(&:month).each do |month, month_orders| | |
orders[item_code][year][month] = month_orders.first.quantity | |
end | |
orders[item_code][year]["total"] = orders[item_code][year].values.map(&:to_f).sum | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
==> Downloading http://downloads.sourceforge.net/project/ghostscript/GPL%20Ghost | |
File already downloaded and cached to /Users/rbates/Library/Caches/Homebrew | |
==> Downloading http://www.ijg.org/files/jpegsrc.v7.tar.gz | |
File already downloaded and cached to /Users/rbates/Library/Caches/Homebrew | |
==> ./configure --prefix=/usr/local/homebrew/Cellar/ghostscript/8.70 --disable-d | |
==> make install | |
/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -I/usr/local/homebrew/include -Wall -Wstrict-prototypes -Wundef -Wmissing-declarations -Wmissing-prototypes -Wwrite-strings -Wno-strict-aliasing -Wdeclaration-after-statement -fno-builtin -fno-common -DHAVE_STDINT_H -DGX_COLOR_INDEX_TYPE="unsigned long int" -mmmx -msse4.2 -w -pipe -fomit-frame-pointer -mmacosx-version-min=10.6 -O3 -I./base -o ./obj/genconf ./base/genconf.c | |
/Developer/usr/llvm-gcc-4.2/bin/llvm-gcc-4.2 -I/usr/local/homebrew/include -Wall -Wstrict-prototypes -Wundef -Wmissing-declarations -Wmissing-prototypes -Wwrite-strings -Wno-strict-aliasing -Wdeclaration-after- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ability | |
include CanCan::Ability | |
def initialize(user) | |
user ||= User.new # guest user | |
if user.role? :admin | |
can :manage, :all | |
else | |
can :read, :all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of nested resource with CanCan | |
class CommentsController < ApplicationController | |
before_filter :load_and_authorize_resource | |
# ... | |
protected | |
def load_resource | |
@article = Article.find(params[:article_id]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Attempting to connect to a C# WSDL | |
>> driver = SOAP::WSDLDriverFactory.new("...").create_rpc_driver | |
ignored element: {http://schemas.xmlsoap.org/ws/2004/09/policy}Policy | |
ignored attr: {http://www.w3.org/2006/05/addressing/wsdl}Action | |
ignored element: {http://schemas.xmlsoap.org/ws/2004/09/policy}PolicyReference | |
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}binding | |
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}operation | |
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}body | |
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}fault | |
ignored element: {http://schemas.xmlsoap.org/wsdl/soap12/}address |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Given these SQL commands */ | |
update books set author="Bob" where id=1; | |
update books set author="John" where id=2; | |
update books set author="Bob" where id=3; |