This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: User-Defined Literals in Sydney
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Evan’s just released another Sydney while the Ruby-Core list is alive with bug reports and input from the core team. Nobu spotted a problem with user-defined literals and I was like, “oh, right, I need to try that.”
Add your methods to the PercentExpansion module. So, if you want your own %y, do like:
require 'yaml'
module PercentExpansion
def y( str, m ); YAML::load( str ) end
end
METADATA = %y{
author: _why
date: 2005-07-13 09:10:12 -05:00
title: User-defined Literals
}
Or for fetching a URI:
require 'open-uri'
module PercentExpansion
def fetch( str, m )
URI.parse( str ).open do |urif|
urif.read
end
end
end
ruby_terms = %fetch{http://ruby-lang.org/en/LICENSE.txt}
Sydney bars you from overriding the default literals. Which is probably safe. But, you know, I have to wonder what sort of hacks we’d see if these could be overridden.
Or if the string itself could be overturned. One thought: interpolation could be restyled in midair. R is for recklesssnessss.