Hacker News new | past | comments | ask | show | jobs | submit login
URLs in Ruby (ozmm.org)
38 points by jmonegro on Dec 14, 2009 | hide | past | favorite | 8 comments



In Common Lisp:

  EPEEN> #u"http://google.com"
  #<URI http://google.com>
Thanks to the PURI library :-)

The twitter example.

  EPEEN> (let ((json (http-request #u"http://twitter.com/statuses/show/6592721580.json")))
	   (cdr (assoc :text (decode-json-from-string (octets-to-string json)))))
  "He nose the truth."

Using puri, cl-json, drakma and babel.


I think the point of the original post is that Ruby can parse URL-like syntax (e.g., http://twitter.com/statuses/show/6592721580.json) as code, no strings attached.


That sounds like it might be a little awkward to parse. I think I prefer the Lisp+PURI syntax, just because you can see where the URL begins and ends easily, and don't have to worry about how the syntax interacts with the rest of the language.


Oh, it's completely awkward, and completely fun. The point wasn't to create a new library to supplant others (if someone used this code in production, I think the joke would be on them), it's to have fun with what's possible in Ruby, and perhaps save a few characters in irb.


It's still putting the url in a string. :)

Perhaps there's a way to do it without any sort of syntax like the quotes or #u using macros?


Something has to be said for the uniformity of syntax. The lisp reader macro is used throughout the language; I have never used this syntax before (I prefer to explicitly make objects, that way I know if the call succeeded right away) but grepped the PURI sources to see if it had a reader-macro, and sure enough it did. If there was no uniformity, I would have waited for a blog post to tell me something exists.

You are complaining a Lisp URL needs to be quoted to distinguish it from other input tokens; how would you feel when you have more than one URL, say 10, on the screen and all of them are being echo'ed in a debug backtrace without visual aid or syntax highlighting? How about if you forget to delimit one with a space (oops, now you have a syntactically valid URL that's broken; the type checker wont help you verify URLs)

Plus you can "merge" Lisp URLs, forming absolute URLs from a base URL and appending paths to it or building query parameters in an elegant fashion.


I totally agree with mahmud about uniformity of syntax and the fragility of doing things like this.

However the original post/gist was just a bit of fun! So here is the same bit of fun in Perl: http://gist.github.com/256992

  use Modern::Perl;
  use Url;
  use JSON qw(decode_json);

  # output the json as is
  say http://twitter.com/statuses/show/6592721580.json;
 
  # => He nose the truth.
  say decode_json( http://twitter.com/statuses/show/6592721580.json )->{text};
First time I've played with Devel::Declare module (http://search.cpan.org/dist/Devel-Declare/) and I found it to be straightforward & an extremely powerful beast for mangling with the perl5 parser in a robust and safe way.


That's quite the REPL prompt...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: