Skip to content

Instantly share code, notes, and snippets.

View pwightman's full-sized avatar

Parker Wightman pwightman

View GitHub Profile
def palindrome? str
str.downcase!
# gsub stands for "global substitution" and replaces all occurrences
# of the matching regex with the given string. Here, we want all "non-word"
# characters to be removed from the string
str.gsub! /[\W]/, ""
# If the forward and backward versions are the same, it's a palindrome
str == str.reverse
end
class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end
def rps_game_winner(game)
raise WrongNumberOfPlayersError unless game.length == 2
player1 = game[0]
player2 = game[1]
player1_play = player1[1]
def combine_anagrams words
# Kendal's idea. Passing a block to the constructor is how you tell
# the hash what to do when a key is accessed that doesn't yet exist.
hash = Hash.new do |h, key|
h[key] = []
end
words.each do |word|
# As the key, we want to use the lowercase sorted
# for of the word, as anagrams will always have the
class Dessert
attr_accessor :name, :calories
def initialize(name, calories)
@name = name
@calories = calories
end
def healthy?
@calories < 200
# (a)
class Class
def attr_accessor_with_history attr
attr = attr.to_s
attr_reader attr
class_eval %Q{
# Create a history method for the attribute
class CartesianProduct
include Enumerable
def initialize a, b
@a = a
@b = b
end
# Every method has an implied block that is sent into it, it does NOT need to be
# specified as a parameter.
1.9.3p194 :006 > u.errors[:title] = "can't be blank"
=> "can't be blank"
1.9.3p194 :007 > u.errors.full_messages
=> ["Title can't be blank"]
1.9.3p194 :008 > u.errors[:base] = "Please don't do that"
=> "Please don't do that"
1.9.3p194 :009 > u.errors.full_messages
=> ["Title can't be blank", "Please don't do that"]
@pwightman
pwightman / rvm_directions.md
Created September 18, 2012 04:11
Installing RVM and Rails

Install Ubuntu on Windows

Mac OSX/Ubuntu users can skip this section. If you're running Windows, follow these instructions to install Ubuntu inside a virtual machine.

  • Download and install VirtualBox from here
  • Download this Ubuntu ISO file (Note that this is 64-bit, if you don't have a 64-bit machine then you'll want to do the 32-bit version
  • Start up VirtualBox and create a new virtual machine for Ubuntu (just follow default options on each screen if you aren't sure what to do. I recommend doing a 40GB hard drive. It won't actually consume that much, it allocates space as it needs it.)
  • Once the virtual machine is created, click on the VM you just created in the list of VMs and click the Settings button
  • In the Storage section, add an IDE controller that points to the .iso file you just down
@pwightman
pwightman / funny_java.java
Created October 4, 2012 21:05
Some straight-up funny Java code.
// This was someone's real java code in production (names changed),
// I'm not making this crap up.
class SomeClass {
public void someMethod() {
if (some_condition) {
try {
throw new Exception();
} catch(Exception e) {
System.out.println(e.message());

IRC for URails

IRC is sort of like a free chat room. Anyone can host their own IRC server and people can connect to it, join "channels", etc. It's often used as a place users can find help with others that have their same common interest. For example, there's a server called "freenode" which is quite popular and has rooms for rails, ruby, python, and most anything you can think of.

Jeff Sandberg, a Rails Veteran who has decided to help out the group, helped set up an IRC server called snoonet that we'll be using for a URails channel that Jeff and I will usually be in when we're at our computers, where you can ask any Rails-related questions and where we can collaborate when we're working on URails stuff at the same time. This will be much easier than the mailing list or email.

IRC Client

Mac