Skip to content

Instantly share code, notes, and snippets.

@alandotcom
alandotcom / gist:3700405
Created September 11, 2012 18:08
Blocks and Procs in Ruby

Originally posted as a private gist

Inspiration: What's that & doing?

Before I get started, the inspiration behind this was a piece of code written to solve a Ruby exercise: Write a method reverse_words which takes a sentence as a string and reverse each word in it.

The solution that set me on a journey of diving deeper into closures was this one, by Daniela Grossman:

	def reverse_words(str)
 str.split(' ').map(&:reverse).join(' ')
@alandotcom
alandotcom / gist:3724382
Created September 14, 2012 20:06 — forked from schneems/gist:2968840
Databases and Rails Recap Quiz for Week 2
## Week 3 Quiz
## 1) What does ORM stand for?
# Put your answers here!
rvm rubies
jruby-1.6.8 [ x86_64 ]
ruby-1.8.6-p420 [ i686 ]
ruby-1.8.7-p370 [ i686 ]
=* ruby-1.9.3-p194 [ x86_64 ]
@alandotcom
alandotcom / gist:3825148
Created October 3, 2012 05:13
Install RVM and Rubies on OSX Mountain Lion
  1. For the first step, there are two options. Choose option A if you plan on developing for iOS. Choose option B to avoid installing several GB of unnecessary software.

    a. Install Xcode 4.5 (it can be found in the App Store)

In Xcode preferences go to the "Downloads" tab and under "Components" push the "Install" button next to "Command Line Tools"  (StackOverflow)

b. Install [Command Line Tools](https://developer.apple.com/downloads) for Xcode 4.5 separately)
 *Note: You will need an Apple ID for downloading Command Line 	Tools*
  1. Install XQuartz
require './binary'
describe 'binary_search' do
let(:int_ary) { (100..200).to_a }
let(:strg_ary) { %w{ swedish\ chef miss\ piggy scooter gnu animal foo-foo }.sort }
let(:one_elemnt_ary) { [0] }
it 'returns correct index' do
binary_search(135,int_ary).should eq 35
binary_search('miss piggy',strg_ary).should eq 3
@alandotcom
alandotcom / card.rb
Created October 21, 2012 18:52 — forked from dbc-challenges/card.rb
FlashCardinator
class Card
attr_reader :term, :definition
def self.load(filename,cards = [])
File.new(filename).each do |card|
card = card.split("\t")
cards << Card.add_card(card)
end
cards
end
@alandotcom
alandotcom / gist:4013119
Created November 4, 2012 19:05
Internal state is not cloned
class Obj
attr_accessor :first, :second
def initialize
@first = {:one => 'x',:two => 'y',:three => 'z'}
@second = {[1,2]=>'x',[3,2]=>'o'}
end
end
# Using either dup or clone doesn't make a difference, they both make shallow copies
obj1 = Obj.new
@alandotcom
alandotcom / jq.konami.js
Created November 6, 2012 02:05 — forked from adamcbrewer/jq.konami.js
JS: Konami Code
// Konami code with jQuery.
// Source: http://paulirish.com/2009/cornify-easter-egg-with-jquery/
var kkeys = [],
konami = "38,38,40,40,37,39,37,39,66,65";
$(document).keydown(function(evt) {
kkeys.push( evt.keyCode );
if ( kkeys.toString().indexOf( konami ) >= 0 ){
$(document).unbind('keydown',arguments.callee);
@alandotcom
alandotcom / fizzbuzz.rb
Created November 16, 2012 00:56
fizzbuzz
=begin
Write a program that prints the numbers from 1 to 100.
But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz".
For numbers which are multiples of both three and five print "FizzBuzz".
(http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html)
=end
module FizzBuzz
def self.fizzbuzz(n)
case
@alandotcom
alandotcom / gist:4142405
Created November 25, 2012 04:31
SuperFeedr PubSubHubbub API inside a Rails App

Structure of the JSON response from SuperFeedr

When working with the SuperFeedr REST API it is important to understand the notifications that SuperFeedr will send to your server.

I am using th JSON response here, as the format is familiar and easy to parse using the Ruby API.

Receiving feed updates

After successfully subscribing to a feed, SuperFeedr will send notifications to the callback URI specified during the subscription process.