I've been working on Habari a bit recently, so have been writing more PHP than Ruby lately. Given that Habari's code quality is pretty good, that hasn't been too terrible. However, I recently had to prepend a URL to a bunch of file paths, and was thinking in Ruby, where I would have done something like this:

url = "http://twofishcreative.com/"
resources = %w[one two three]
resources.map! { |resource| url + resource }
puts resources

The beauty of this is that it keeps the callback code with the calling code, making it easier to read. PHP doesn't have blocks, but it does have the create_function() function, which lets you create an anonymous function, so you can have your callback inline too.

$url = "http://twofishcreative.com/";
$resources = array("one", "two", "three");
array_walk($resources,
create_function('&$resource,$k,$url',
'$resource = $url.$resource;'), $url);
print_r($resources);

Ouch! That quoted function! Deity forbid you actually want to do anything complex in there. And you get a completely new scope, so you have to pass in any variables you need. And if you want to use more than one variable you need to do some horrible kludge like passing an array of variables.

I'll leave it as an exercise for the reader to work out which I prefer.

Very few people write in assembly language today, at least not entire applications. We've all since moved up the stack of abstraction to using C, or one step above that into Perl, Python, Java, etc. The answer is to keep moving up the stack to higher levels of abstractions, such as 'channels'. In a way I wish that Moore's Law would grind to a halt and we could get back to doing computer science.
[read more]
I've written here and here about serving static files with Camping, but each time I've set up a new route for each resource type. You can serve all static files with a single route by following the instructions on the wiki. Here's the code that's presented there. module Camping::Controllers class Static < R '/static/(.+)' TYPES = {'.css' => 'text/css', '.js' => 'text/javascript', '.jpg' => 'image/jpeg'} PATH = File.expand_path(File.dirname(__FILE__)) def get(path) @headers['Content-Type'] = TYPES[path[/\.\w+$/, 0]] || "text/plain" unless path.include? ".." # prevent directory traversal attacks @headers['X-Sendfile'] = "#{PATH}/static/#{path}" else @status = "403" "403 - Invalid path" end end end ...
[read more]
Camping is great, yeah. JQuery too. Maybe you want to use them together? Here's how. First, you need to set up a route for your JQuery. I've talked about sending static files with Camping before, so this is just a modification of that. [You can serve static files more sensibly than setting up a route for each type. Maybe one day I'll write about that.] In your controller: module MyApp::Controllers class Index < R '/' def get render :index end end class JQuery < R '/resources/jquery.js' def get current_dir = File.expand_path(File.dirname(__FILE__)) @headers['Content-Type'] = "text/javascript" @headers['X-Sendfile'] = "#{current_dir}/resources/jquery.js" end end end ...
[read more]
PHP's errors have always struck me as being particularly impenetrable. There doesn't seem to be any explanation of the errors on the PHP site either. What the hell does Parse error: syntax error, unexpected ';', expecting T_FUNCTION in blah.php on line 145 mean? Well, the most likely explanation for that error is that you forgot the close brace on a class, so you have a function close brace immediately followed by ?>.
I was excited to learn recently that the Nokia N73 can speak AtomPub, and that a friend of mine owns one. I thought I'd try to make it talk to the new AtomPub implementation in WordPress, but reading through the N73 documentation I found that it only supports WSSE authentication, and WordPress only speaks HTTP Basic Authentication. I'd never heard of WSSE, but Mark Pilgrim has a good write up on XML.com, and the Ape has the ability to speak WSSE, so I thought I'd implement it in WordPress. Bear in mind that I'm not writing this from a ...
[read more]
Camping interprets every request to the server using the paths in the controllers, so if you want to serve static files, like external stylesheets or images, you need to provide a controller for them. Take this bit of Markaby: img :src => 'images/logo.png' This will get interpreted by Camping and end up with this HTML: <img src='images/logo.png' /> When the browser reads that, it will send a GET request to the Camping server looking for logo.png, that Camping will try to match against the controller paths. If you haven't set one up, you'll get an error. So, in your ...
[read more]
The title says it all really. I'm using hpricot for the first time, and the page I'm scraping has quaint uppercase HTML elements, shouting its TABLEs at me. It took me quite a while to work out that I didn't have to shout at hpricot.
In PHP 5.2 the __toString() method is used to convert an object to a string. In PHP 5.1 the __toString() method is used to convert an object to a string but only when print or echo is called on the object. So, doing something like this won't work in 5.1: foo = bar('passing a string to a function '.$object.' won't convert the object to a string');
I've just installed vcscommand.vim, which integrates version control into vim. I was trying it out and was getting the error "No suitable plugin" whenever I ran a command. Turns out this was because I wasn't actually in a working directory, I was in the directory I initially imported. So, remove the directory and then checkout the source, now all good.