Skip to content

Instantly share code, notes, and snippets.

@defunkt
Created November 24, 2009 21:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save defunkt/242228 to your computer and use it in GitHub Desktop.
Save defunkt/242228 to your computer and use it in GitHub Desktop.
Mustache method_missing hack
require 'mustache'
class Ranger < Mustache
self.template = <<-template
{{#range_0_to_10}}
<option value="{{i}}">{{i}}</option>
{{/range_0_to_10}}
template
def range(first, last)
(first..last).map { |i| {:i => i} }
end
def method_missing(name, *args, &block)
return super unless name.to_s =~ /^range_(\d+)_to_(\d+)/
range($1, $2)
end
def respond_to?(method)
method.to_s =~ /^range_(\d+)_to_(\d+)/
end
end
puts Ranger.render
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment