This lovely piece of code from Ryan Dahl's JSConf presentation in 2009 illustrates so beautifully the nature of node.js:
var sys = require("sys");
setTimeout(function() {
sys.puts("world");
}, 2000);
sys.puts("hello");
Some node.js references: Bulletproof node.js coding Node.js best practices
Sometimes on a corporate network, you'll be stuck behind a proxy and working away on a Windows box. Trying to install or update gems like Ruby on Rails with commands like
gem install rails
can give you strange errors. You'll see various things at various points of bashing your head against the wall. Things like
ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)
bad response Forbidden 403 (http://rubygems.org/latest_specs.4.8.gz)
or
ERROR: Could not find a valid gem 'rails' () = 0) in any repository
ERROR: While exectung gem ... (Gem::RemoteFetcher::FetchError)
bad response Forbidden 403 (http://rubygems.org/latest_specs.4.8.gz)
How do I fix this?
Personally, I tried calling the inhouse IT department to try and get things unblocked. They couldn't really help me out. So instead I grabbed a copy of ntlmaps which basically allows you to create and run a proxy on your local machine that will grab all requests and re-package them as if they were coming from IE.
ntlmaps is a neat little python utility so you'll need to have that installed. Read the Install.txt and readme.txt to get a little background and basic info. I changed my server.cfg file filling in the following bits:
NT_DOMAIN:superCorp
USER:calvin
PASSWORD:hobbes
LM_PART:1
NT_PART:0
Now I've got a nice little local proxy ready to run - run it with runserver.bat at the command line - and wait for it to fire up.
You should get something like
NTLM authorization Proxy Server v...blahblahblah
Now listening at COMPUTERNAME on port 5865
COMPUTERNAME is going to be your computer name.
Now, open a new command shell and type in
SET HTTP_PROXY=http://COMPUTERNAME:5865
You'll get no response from that command but don't worry
At last, you can try updating the gem again:
gem update rails
And you should be good to go.
Very cool - sort algorithms expressed as dance:
Bubble sort:
Select sort:
Shell sort:
Insert sort:
I find it interesting that some web applications have matured to the point that they basically become frameworks in and of themselves. Take WordPress as an example. Originally written as a blogging tool, it has evolved into a framework of its own through its various hooks and actions that, if you were feeling adventurous, allow you to do pretty much whatever you want, from creating new tables to creating entirely new functionality.
Playing with a system that's evolved into a framework is very different to playing with a system that was written as a framework first and foremost. I've been developing a plugin for vanillaforums to allow classified listings on a forum site, and it's interesting to look at the architecture. On some levels, vanillaforums feels over-complicated for just a forum. But then you look a bit deeper and find a whole MVC framework coded up, with the forum module essentially just an application written on top of the framework.
I wonder at what point we could consider an application to be a framework? Because any well designed system is or should be easy to extend. I think what makes an application into a framework is documentation. Good documentation that allows moderately skilled developers to jump in and be productive immediately turns a simple app into a framework in it's own right. That and a .org domain name :)
I remember Logo well. I played with it a lot when I was still very young. So a nice post about corrupting the youth inevitably found me stumbling over to youtube and a talk by Alan Kay.
The gist is that 5 year olds can come up with a "program" to create a circle, whereas 15 year olds cannot. Now there is a trick to it, which is so obvious that you kind of hit your head when you get it, but more than the trick, it shows that very often we follow a train of thought without really first sketching out a few alternatives. Sometimes it is good to stop and really examine what we are trying to achieve and really develop our solution based on that.
I heard a lovely Winter North Atlantic snippet on a Digitonal mix called Big Chill Summer Mix. This is not it though.
Here's a lovely little one that just took me much to long, probably because I'm tired, or having an off day or something:
Select [Name]
[Surname],
[Address],
[Telephone],
[Email]
From ATableAboutPeople
See, the error isn't obvious, because the SQL compiles and runs, but the result set just doesn't bring back what you expect.....because [Name] is now aliased as [Surname] - all because of a missing comma.
Hate it when that happens.
If you jump between Linux and Windows XP like I do, sometimes you'll pull your Rails app into the dreaded M$ platform, fire it up and be greeted with a lovely "Internal Server Error". Naturally it goes on to say something like "You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each" despite the fact that you've changed no code since last night when your app was running rather nicely in a Linux environment thank you very much. Sometimes you just need to hack your way out of a problem without worrying about the whys and wherefores - and that's exactly what we are going to do here. So......open up configenvironmentsdevelopment.rb and find this line :
config.logger = Logger.new(config.log_path, 2, 10.kilobytes)
</code>
comment it out comme ca
```ruby
#config.logger = Logger.new(config.log_path, 2, 10.kilobytes)
and fire it up again. All good? Yes? Excellent. Fixed. Why? Um. Log rotation in XP apparently. Something funny about it. Don't really know. Haven't investigated it much further because there is no way in hell I'm going to run a production Rails app in Windows. Ever.
Yes indeed folks, I bring you the number of passports lost or stolen in Europe as recorded by the British consulates in said countries for the year 01 April 2008 to 31 March 2009.
Red = worse. From which we can falsely infer that we shouldn't go to the red ones because you are more likely to have your passport stolen :)
One of the blessings in Wordpress is the vast array of plugins available. However, it is also one of the curses....because it makes us lazy. While this doesn't seem to be problematic in the short term, in the longer term, large numbers of plugins can cause blogs to slow down, and even worse, the various plugins could start interfering with each other. So, whenever I can, I like to write my own code - the two advantages are that
1 - I can control exactly what goes in to the code, making it do what I need and no more and 2 - I've written the code so if things start to clash, it's going to be easier to debug.
So, let's learn how to create a Wordpress page to display every comment on our blog and just for fun, let's also code up a quicktag which we can place into any page or post where we want the comments displayed.
Start in your theme's functions.php
add_filter('the_content', 'all_your_comments_are_belong_to_me');
function all_your_comments_are_belong_to_me($content) {
if (strstr($content, '<!--all_your_comments-->')) {
$allcomments = get_comments();
$strout = "<ul>";
foreach ($allcomments as $comment) {
$postforcomment = get_post($comment->comment_post_ID);
$strout .= '<li><strong><a href="'. $comment->comment_author_url . '">' . $comment->comment_author . '</a> had a thought about <a href="'.get_permalink($comment->comment_post_ID).'">'.$postforcomment->post_title.'</a></strong>';
$strout .= ' on <small>' . $comment->comment_date . '</small><br>';
$strout .= '<p>' . $comment->comment_content . '</p></li>';
}
$strout .= '</ul>';
$content = str_replace('<!--all_your_comments-->', $strout, $content);
}
return $content;
}
What's all this code doing? Well first, we set up a filter with the add_filter command. Filters allow us to extend much of the core Wordpress functionality, allowing us to add our own processing. This is very powerful because it means we can modify the way Wordpress saves or displays our data without having to delve into the core Wordpress code and hack our Wordpress install to pieces. That in turn means that we can safely update the core of our blog without having to worry about re-applying any customised functions that we have written for our blog. Filters are what makes plugins work like magic.
So for this bit of magic, we are adding a filter to a hook called the_content. This tells Wordpress that we want to execute some of our own code whenever Wordpress is creating the content of a page or post just before it is displayed on the screen. The function we are going to call is all_your_comments_are_belong_to_me which we define right after the add_filter command.
This function takes one argument by default - $content (or whatever you want to call it) which is the actual written content of your post or page (for example you are reading the content of my post right now). We wrap everything in an if statement - which means that we will only process the rest of our code in our function if somewhere in our content we have the following quicktag: all_your_comments.
Okay, into the meat and potatoes - getting every comment to display. Here we use a tasty function called get_comments() which, as the name suggests, gets all the comments for every post in our blog. There are various arguments you can supply to this Wordpress function, but we really don't need any of them for the purposes of our demo. Now we loop through all the comments, creating a string that is the actual html of all the comments we want to output. At the same time, to add a little extra value we grab the post associated with each comment. This is a little bit more costly in terms of actual database round trips, but what the heck, we want a nice output.
We create our string (format it however you want and define your styles in your css. And then, the pièce de résistance - the str_replace which says: "take this lovely html string I have created and substitute it into our $content variable wherever you find the phrase <!––all_your_comments––>". Remember $content is the actual written content of our page. Finally, we return our $content variable from our function.
One gotcha - when using a hook into the_content, you must return something, so at the very least, if we fail our if condition we just send back the $content that we got in. If you don't, Wordpress will display nothing - no content for your post. Be warned.
Okay now all that's left is to write a new page to display all these comments - whip up a lovely new page or post and write it up as you wish. At any point, slap in the all_your_comments quicktag and save.
This code is both good and bad. Good in that it is quick to code up but bad in that every time Wordpress displays a post or page, it will go through our function - this will slow down our blog (but hey, I'm taking one for the team on the demo page). So how else could we do this? Well we could create a template page which would do this for us - this would be more efficient, but less flexible because we wouldn't be able to control where the comments were injected into the contents of the page.
So, I hope you got something out of that. This can easily be extended into a plugin if you are feeling that way inclined; perhaps I'll do that at some point in the future. For now though, hopefully you enjoyed that little step into Wordpress hacking and have a better understanding of how easy it is to code up a custom quicktag in Wordpress to pull in all of the comments in your blog.