This file contains hidden or 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
| # 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' |
This file contains hidden or 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
| 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 |
This file contains hidden or 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 Game | |
| attr_reader :rolls | |
| def initialize(rolls) | |
| @rolls = rolls | |
| end | |
| def score | |
| frames.inject(0) do |score, frame| | |
| score += FrameScorer.new(frame).score |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| ~/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 |
This file contains hidden or 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 PrivacyFilter | |
| def self.filter(controller) | |
| [:first_article?, | |
| :authenticate_administrator!, | |
| :authenticate_user! | |
| ].find do |m| | |
| controller.send(m) | |
| end | |
| end | |
| end |
This file contains hidden or 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
| # 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. |
This file contains hidden or 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
| <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> |
This file contains hidden or 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
| # 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 |
This file contains hidden or 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
| 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 |