Created
June 20, 2010 06:15
-
-
Save mtodd/445605 to your computer and use it in GitHub Desktop.
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
module Enumerable | |
def each_with_context(&block) | |
block.call([nil, self[0], self[1]]) | |
self.each_cons(3, &block) | |
block.call([self[self.size - 2], self[self.size - 1], nil]) | |
self | |
end | |
def enum_with_context | |
Enumerable::Enumerator.new(self, :each_with_context) | |
end | |
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
require 'rubygems' | |
require 'activesupport' | |
class User < Struct.new(:id, :name) | |
def self.find(n); self.new(n, "Matt"); end | |
end | |
class Post < Struct.new(:id, :name) | |
def self.find(n); self.new(n, "HooHah"); end | |
end | |
class Comment < Struct.new(:id, :name) | |
def self.find(n); self.new(n, "Sweet"); end | |
end | |
url = "users/1/posts/2/comments/3" | |
url.split('/').each_with_context do |(prev, element, _)| | |
case element | |
when /^\d+$/ | |
p [:element, prev.singularize.camelize.constantize.find(element).name] | |
else | |
p [:element, element] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment