User-Defined Literals in Sydney #
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.
netghost
Are redefined literals really a good idea? Is
%fetch{http://ruby-lang.org/en/LICENSE.txt}
Really much better than
fetch "http://ruby-lang.org/en/LICENSE.txt"
Perhaps there is a place for them with stuff such as inlining xml, sql, javascript and the like. At the moment though they just seem to add some more syntax.
why
It’s an example. I really like
%y
, but yeah%fetch
is probably contrived.riffraff
I have to admit I want %stuff to disappear. Except for %w, I don’t feel they add so much, and they feel strange for everything else. But user defined %stuff is better than now :)
murphy
Really cool idea. Sounds like Ruby**2. This as language feature would be extremely interesting.
I always use ` to define string-like literals in my code. It’s not just another way to call a function, because is carries additional information.
Think further. What about %R/bla/ for a shorthand to /\Abla\z/, something I always need to check input. Even modifiers could be added to strings or system calls.
Or what about a syntax like
for text2html conversion, and
for reverse. Or regexp heredocs. Or ANSI colors with
Oh the possibilities. Enhanced string features in Ruby are so useful (I always get angry when I’m lost within Java or C that don’t have % or <<- or #{}.) But there is no reason to stop here. As long as is stays dynamic, intuitive and readable (I think %-notations increase readability), I will surely use syntax sugar of that kind.
But I assume matz won’t add it; maybe it’s too much noise.
hgs
What’s the
m
for? What must itrespond_to?
?MenTaLguY
How about we just make grammars a first-class abstraction in Ruby, and permit Ruby’s grammar to be arbitrarily extended a la Perl 6? </evil>
why
I’m sold on this. Check this out:
You’ve got to admit, you’ve always wanted to have strings that automatically
gsub( /^ +/, '' )
, right?flgr
riffraff: I also use %r{} a lot because escaping slashes is totally way too ugly. I think that percent literals are over all still a good thing—just don’t use them just because you can unless you are writing code that is supposed to be hard to understand…
netghost
But do you really gain anything over defining ‘red’ in the kernel and then using:
I think part of the power of ruby’s syntax is that you can introduce things like this right now.
riffraff
netghost: mind you, triple quotes are a python thing they work in ruby just by accident ;)
MrCode
All this talk of %-quoting inspired me to write the following (which is pure-Ruby, not Sydney-Ruby):
Feel free to run it, it doesn’t do anything evil.
P.S. Yes, I’m training for next year’s obfuscated Ruby contest.
murphy
Mmh…I noticed that the Kernel method variants have a big advantage: They are not limited to literals.
If PercentExpansion was common, people would start to write
or something. That’s not good.
How about expanding String instead?
nornagon
Well done, MrCode! Now write a brainfuck interpreter in one line.
nornagon
murphy:
:D
On a side note, in %r(#{string}) (for example), are ( and ) escaped?
gmosx
this is a great feature, a bit like Lisp Read macros! Hope this gets includeded in Ruby 1.9
Evan
Peter
Noticed this way too late. There’s another implementation of user-defined literals available at http://developer.berlios.de/projects/suby/. There are a few differences with Evan’s version, but this patch should reflect how I envisioned user-defined percent literals when I wrote the RCR (though it does not handle things like %red yet, only single character names like %r).
And to MenTaLguY: we are toying with the idea of making grammars first-class objects in Suby (another Ruby fork, see http://developer.berlios.de/projects/suby/, though the web page certainly needs an update) and allowing Ruby’s grammar to be extended arbitrarily, but mostly due to time shortage the work on Suby is often stalled, and happens in small bursts.
Comments are closed for this entry.