Skip to content

Instantly share code, notes, and snippets.

@tadman
Created May 26, 2010 15:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadman/414677 to your computer and use it in GitHub Desktop.
Save tadman/414677 to your computer and use it in GitHub Desktop.
# This relates to the issue described at:
# https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4709
class String
def %(args)
if args.kind_of?(Hash)
dup.gsub(INTERPOLATION_PATTERN_WITH_ESCAPE) do |match|
if match == '%%'
'%'
else
key = ($1 || $2).to_sym
raise KeyError unless args.has_key?(key)
$3 ? sprintf("%#{$3}", args[key]) : args[key]
end
end
elsif self =~ INTERPOLATION_PATTERN
raise ArgumentError.new('one hash required')
else
result = gsub(/%%?([{<])/, '%%\1')
result.send :'interpolate_without_ruby_19_syntax', args
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment