Skip to content

Instantly share code, notes, and snippets.

View steveklabnik's full-sized avatar
🦀
Rustacean

Steve Klabnik steveklabnik

🦀
Rustacean
View GitHub Profile
@steveklabnik
steveklabnik / maze.rb
Created December 5, 2011 00:30
For posterity
# This code solves one of Mike Amundsen's maze+xml mazes: http://amundsen.com/media-types/maze/
#
# The code isn't great. I just wanted to see how easy it'd be. So I built up a little test, extracted
# some stuff into a method... next thing you know I've got a little procedural application.
#
# Backtracking implementation borrowed from https://github.com/caelum/restfulie/blob/master/full-examples/mikemaze/maze_basic.rb because I'm lazy.
require 'uri'
require 'net/http'
@steveklabnik
steveklabnik / frames.rb
Created December 5, 2011 03:23
Bowling game frames
def frames
Enumerator.new do |yielder|
position = 0
10.times do
yielder << rolls[position,3].map(&:to_i)
if rolls[position] == 10
position += 1
else
@steveklabnik
steveklabnik / bowling.rb
Created December 5, 2011 03:46
Bowling kata
class Game
attr_reader :rolls
def initialize(rolls)
@rolls = rolls
end
def score
frames.inject(0) do |score, frame|
score += FrameScorer.new(frame).score
@steveklabnik
steveklabnik / anti_if_abuse.rb
Created December 5, 2011 04:17
Anti-if abuse, haha!
def frames
Enumerator.new do |yielder|
position = 0
10.times do
yielder << rolls[position,3].map(&:to_i)
position += next_position(rolls[position])
end
end
~/tmp [ time rake -T ] 4:20 PM
rake -T 6.39s user 0.23s system 134% cpu 4.905 total
~/tmp [ ruby -v ] 4:20 PM
rubinius 2.0.0dev (1.8.7 a33141ed yyyy-mm-dd JI) [x86_64-apple-darwin10.8.0]
~/tmp [ rvm use 1.9.3 ] 4:23 PM
Using /Users/steveklabnik/.rvm/gems/ruby-1.9.3-p0
~/tmp [ time rake -T ] 4:23 PM
Invalid gemspec in [/Users/steveklabnik/.rvm/gems/ruby-1.9.3-p0/specifications/term-ansicolor-1.0.7.gemspec]: invalid date format in specification: "2011-10-13 00:00:00.000000000Z"
rake -T 0.32s user 0.05s system 31% cpu 1.188 total
~/tmp [ rvm use 1.9.2 ] 4:23 PM
@steveklabnik
steveklabnik / privacy_filter.rb
Created December 12, 2011 04:02
A spec for a filter
class PrivacyFilter
def self.filter(controller)
[:first_article?,
:authenticate_administrator!,
:authenticate_user!
].find do |m|
controller.send(m)
end
end
end
@steveklabnik
steveklabnik / ability.rb
Created December 12, 2011 13:04
Wtf cancan?
# Article is a normal AR class, no methods, just attributes. free is a boolean attribute.
#
# /articles/1 is free, /articles/2 is not.
#
# I want only logged in users to read non-free articles. But the :free => true line seems to be enabling
# reading all of them; when I comment it out, non-logged-in users can't read anything. But with it
# uncommented, they can read everything. WTF?
#
# User.new.is? :user is false, and so is User.new.is? :admin.
@steveklabnik
steveklabnik / shallow-vs-deep.html
Created December 14, 2011 21:29 — forked from jonm/shallow-vs-deep.html
Shallow vs. deep representations in XHTML Hypermedia APIs
<html>
<body>
<!-- the following div is a sample representation of a 'car' domain object; it can be identified as
such by the presence of 'car' in its @class. In this case, the car has two attributes, a make
and a model, and both are included right here. This is what I call a deep/complete/concrete
representation. -->
<div id="car123" class="car">
<span class="make">Ford</span>
<span class="model">Mustang</span>
</div>
@steveklabnik
steveklabnik / total_bullshit.rb
Created December 27, 2011 03:52
This is some random bullshit about non-nullable stuff in Ruby.
# I'm making all this up. Just random thoughts. It's 11pm, this probably wouldn't even work.
class Object
@nullable = false
def nullable?
@nullable
end
@steveklabnik
steveklabnik / regexp.rb
Created January 9, 2012 18:40
I think this spec implies that I should commit myself into some kind of rehab.
it "lists them in reverse chronological order" do
recent_titles = Article.for_dashboard.map(&:title).map{|t| Regexp.quote(t) }
title_regex = Regexp.new(recent_titles.join(".*?"), Regexp::MULTILINE)
page.html.should match(title_regex)
end