Array(nil) => []
Array([]) => []
Array(1) => [1]
Array([1]) => [1]
def do_stuff(model_or_models)
models = Array(model_or_models)
models.each do |model|
# do stuff...
end
end
Thursday, October 23, 2008
Method of the Day: Array()
Another oldy but a goodie...
Array() wraps its parameter in an array. If it's already an array, it just returns it. If it's nil, it returns an empty array. It's super useful when you want to write a method that can take a single instance of an object or an array of objects.
Wednesday, October 22, 2008
DelSolr 0.0.2 Released - Added Support for Updates/Delete
A while ago I posted on the first version of delsolr (0.0.1) which provided support for querying solr and not much else :).
We just released an update to delsolr (0.0.2). The major change is added support for updating/deleting documents. It's still fairly untested, so, standard disclaimer applies.
c = DelSolr::Client.new(:server => 'solr', :port => 8983)
# updating one document...
doc = DelSolr::Document.new
doc.add_field('id', 'ABC-123')
doc.add_field('name', '8 Gig Shuffle')
doc.add_field('description', 'A crazy-tiny MP3 player')
c.update!(doc) # posts new document to solr
# updating a whole bunch and batching the post to solr
models = SomeModel.find(:all)
models.each do |model|
doc = DelSolr::Document.new
doc.add_field('id', model.id)
doc.add_field('name', model.name)
doc.add_field('description', model.description)
c.update(doc)
end
c.post_update! # post all the updates in one batch
c.commit! # post a commit to solr
I used a rails example, but delsolr is really intended for ruby and not necessarily rails. Acts_as_solr and the like might suit some better (but not us).
This project came out of a piece of code we use internally to manage solr connections. My goal is to get delsolr to the point where we can replace our old implementation. It's not there yet, but it's getting closer. (It's slow going as this is largely done in our "free time".)
Tuesday, October 14, 2008
Introducing fuzzy-find-in-project: TextMate-style file browsing in Emacs
Moved here
I've been trying off and on to learn to use Emacs over the last few months, but I always found myself coming back to TextMate for one reason: the "Command-T" Find File in Project command. I tried a few of the plugins for Emacs that were supposed to replicate the functionality, but none of them had the performance to quickly search through thousands of files. I had just about given up when I saw Jamis Buck's post about porting an enhanced version of TextMate's functionality to a rubygem, which he could wrap with a vim plugin. A few days later, I wrote my own wrapper to the gem for Emacs as a way of learning Emacs-Lisp, and posted it to GitHub: fuzzy-find-in-project. It works pretty much like Jamis' vim plugin, and has been pretty fast as far as I can tell (at least, much faster than the pure Elisp plugins I tried). Here's how it works:
Run the command
Narrowing down the possibilities
Selecting the next match
Opening the matched file
You can modify the project directory with the function:
(fuzzy-find-project-root "~/path/to/project")
Check it out!
Subscribe to:
Posts (Atom)