Dynamic Class Methods in Ruby

Rails has a whole slew of class methods that really should be instance methods. The most notable of these methods is ActiveModel::Naming‘s model_name, which is a class method that is expected on every ActiveModel object (for example, it is expected by form_for). This code uses that as an example, but the pattern applies anywhere. If […]

An Infinite List of Fibonacci Numbers in Ruby

So I was reading through the Haskell Prelude when I stumbled across `scanl’ as a kind of abstraction over `foldl’. I stared, and thought, and stared some more, and couldn’t come up with a use for it; a quick Web search revealed exactly one use: Fibonacci numbers. (In retrospect: duh.) So here are all the […]

Powerset in Ruby Using the List Monad

While trying to think of good, quick interview questions I stumbled across power set (thanks to Mike DiStaula for suggesting it). After 10 minutes of hacking in Ruby I discovered that it’s not a reasonable replacement for our current starter question (it takes too long), but it’s also a fun problem to solve. Then I […]

Uniquify an array of hashes in Ruby

Array#uniq is a handy Ruby method that produces an array of distinct elements. So, for example, [1,2,3,3,3].uniq produces [1,2,3]. However, this fails on an array of hashes. For example, [{}, {}].uniq actually produces [{},{}]. That’s not what I wanted, recently. It turns out that #uniq uses the methods #hash and #eql? to determine uniqueness, and […]

Spider bugfix

There were two issues with version 0.4.0 of Spider, both caught by Henri Cook. These are now fixed in 0.4.1: As documented, you use IncludedInMemcached like this: require ‘spider/included_in_memcached’ . Sometimes HTTP redirects assume a base URL; this is now handled.

Spider with memcached

The problem with Spider has been that it can use all your memory. The reason is that the Web is a graph, and to avoid cycles Spider stores each URL it encounters. Since the Web is a really, really, really gigantic graph, you eventually run out of memory. Now you can use memcached to use […]

Spider: API changes, setup and teardown, HTTP headers

The newest version of Spider, 0.3.0, is hitting your gem tree Real Soon Now. This release features: Set the headers to a HTTP request. This can be used to set the cookies, user agent, and many other fine things. setup and teardown handlers. Seems like a good place to set the headers if the headers […]

Spider bug fix release

John Nagro immediately reported errors with the Spider Ruby gem, so I’ve fixed them in 0.2.1. You should upgrade, especially if you want support for: URLs without any path component (e.g. http://example.com?s=1). HTTP redirects. HTTPS. John also had some good ideas, so here is what is in the works: The ability to construct a complete […]

An updated way to spider the Web with Ruby

I’ve released version 0.2.0 of Spider. Everything has changed: Use RSpec to ensure that it mostly works. Use WEBrick to create a small test server for additional testing. Completely re-do the API to prepare for future expansion. Add the ability to apply each URL to a series of custom allowed?-like matchers. BSD license. The new […]

Yahoo! Local vs. Yahoo! Local’s API

Yahoo! Local is pretty awesome; it can tell you all the stores named “Shaw’s” near the zip code 02215, for example. Yahoo! Local’s API, however, gives different results depending on its mood— sometimes the Shaw’s in 02215 is returned second; sometimes fifth; sometimes not at all. Try this URL instead: http://api.maps.yahoo.com/ajax/locsrch It takes these parameters: […]