Archive for the ‘Programming’ Category

PHPUnit on OSX Lion CLI

Saturday, November 10th, 2012 | Programming, Tech

If you’re trying to run PHPUnit from the terminal in Mac OSX Lion, you may get an error similar to the following.

File/Iterator/Autoload.php failed to open stream

You can resolve this by running the following commands.

curl http://pear.php.net/go-pear.phar > go-pear.php
sudo php -q go-pear.php

PHPUnit should now run without errors (or at least, without errors in their code 😉 ).

Embedding other templates in Sinatra ERB templates

Sunday, November 4th, 2012 | Programming, Tech

If you are using the Sinatra framework in Ruby, and you want to embed one ERB template in another, you can do this simply by using the ERB template tag like you can in your regular Ruby code.

<%= erb :welcome %>

Replace “welcome” with your template name as you usually do.

Cassandra RubyGem on Mac OSX Lion

Monday, October 29th, 2012 | Programming, Tech

If you’re trying to run Apache Cassandra with the RubyGem, you may encounter an error similar to the following.

thrift_client(0.8.1 not ~> 0.7.0)

You can fix this by editing the RubyGem.

cd /Library/Ruby/Gems/1.8/gems/cassandra-0.12.1/lib/
vim cassandra.rb

Find the line which sets the dependency.

gem 'thrift_client', '~> 0.7.0'

Change it to the following.

gem 'thrift_client', '~> 0.8.0'

Your script should now run without an error.

PHPNW12

Thursday, October 18th, 2012 | Events, Life, Programming

As part of my push to attend more conferences this year, and get out into the real world, I recently attended PHPNW12, a PHP developer conference that took place in Manchester.

I arrived on the Friday night and checked into the hotel across the road from the conference, the Britannia. With it’s sweeping balconied staircases it felt like I was in a 70s horror movie. The floors creaked and the light in the corridor outside my room flickered on and off constantly – indeed, it rather ruined the mood when they fixed it.

The Friday night featured a hackathon, though not feeling too well due to the tail end of a cold, I spent about 20 minutes hacking, then ate my pizza while I checked my emails for an hour and headed to bed, not to emerge until 12 noon the next day when I felt a bit better.

The talks were on the whole good – there was a real range in there, some had really interesting topics but due to their lack of experience presenting talks, where rather dull. Others were confident and entertaining speakers who despite presenting quite dull topics (caching is not going to be mega interesting) presented brilliant talks. On balance, I would certainly prefer them to focus more on speaker quality over topics next year.

On the Saturday night there was a social including dinner, at which I spent quite a bit of time getting to know some of the other people at Sky – I didn’t realise they were going as I hadn’t gone with Sky, but it was great to see some familiar faces there.

Overall, I found I learned a lot from it. If I can bring back just a few ideas to my own business then it will have been worth the expense.

Staircases in the hotel. I also tried the new panorama function on iOS6, on it’s side:

What variables do in private is their own business

Saturday, September 29th, 2012 | Limited, Programming

This is another post about object oriented programming.

Ok, cool, you’re one of the 3% still reading. I just wanted to do a quick post about access modifiers on objects. It applies pretty much regardless of programming language, presuming you’re working with one that supports OO and has the standard public, private and protected for access by sub-classes only.

I think, what we need here is an attitude shift away from using private variables. Often, I see code that uses private variables and I have no idea why.

What I mean by this, is that almost every time I see a private variable, what it actually should be is a protected variable. Indeed, I think the default assumption when creating a variable, should be that you define it as protected.

Lets ignore public variables for the moment. I’ve previously argued you could simply do away with them altogether (that is what getters and setters are for), but in any case, I’m not concerned with them for this post. Lets just focus on private or protected.

The traditional teaching has always been that you should define a variable as private if you don’t want it to be publicly accessible, and giving child classes access to it later on is often an afterthought.

I don’t think this should be the case.

If we’re to adopt a true OO mindset (and it’s been around for sixty years, so given it was invented before most of us were born, you would hope we would have adopted it by now), surely you would work from a perspective that your class will be extended.

Protecting variables from external bodies makes sense, hence not making them public, but to by default place restrictions on what you can do with child classes in an OO world, doesn’t make sense to me. Why have the functionality at all? Why not make everything final if you need such protection?

That isn’t to say there aren’t plenty of instances where there is a reason to do this – but these aren’t the 90% most common use cases, so I think there is a good argument for making protected the default at least.

Install Memcache on CentOS 6 cPanel

Tuesday, September 11th, 2012 | Programming, Tech

Here is how to install Memcache for PHP on a CentOS 6 cPanel / WHM box. Some of the guides suggest that you need libevent (well, you do need libevent), though when I tried it, I already had it installed. But if you need it, yum will sort you out.

yum install libevent

Next, install memcache itself. Note that the package is called memcached.

yum install memcached

Of course, just installing it doesn’t mean that the daemon is running. So don’t forget to start it too!

/etc/init.d/memcached start

Finally, we need to add the PHP extension. Beware that the PECL installer on WHM won’t work! So you need to compile it manually from source. That isn’t too difficult though.

wget http://pecl.php.net/get/memcache
tar zxvf memcache
cd memcache-3.0.6
phpize
./configure
make
make install

And add the extension to your php.ini.

extension=memcache.so

Now restart Apache and a memcache section should appear in your PHP info.

Add Scala to your system path on Mac

Wednesday, September 5th, 2012 | Programming, Tech

Lets say you are running Mac OS X Lion and you have installed Scala to /usr/local/scala. To access scala from the terminal you need to do the following.

cd /etc/paths.d/
sudo vim scala

Add one line with the following in.

/usr/local/scala/bin

Save the file and restart terminal. You’ll now be able to use scala from the terminal.

JavaScript’s querySelector

Sunday, September 2nd, 2012 | Programming

jQuery has an amazing set of selectors, but what happens if you don’t have access to jQuery?

It happens, especially if you’re writing the automated tests or working within an existing framework and don’t necessarily have control over the content of the page. Luckily, you don’t actually need jQuery!

All modern browsers support the use of querySelector. When I saw all modern browsers, I even include Internet Explorer which added basic support for it in 8 and full support in 9. You use it like this.

document.querySelector(“p.introduction”);

This works just like jQuery – selecting all paragraph tags with the class introduction.

ActiveRecord::ConnectionNotEstablished in Rails

Friday, August 24th, 2012 | Programming, Tech

I ran into this error while trying to get Ruby on Rails 3.1.1 which I installed from the Rails Installer to talk to MySQL. My stack is built from WAMP, so it might not have been as easily as it would be on a standlone MySQL installation.

Here is are the steps I took to fix it.

Edit Gemfile in project and add the following line.

gem 'mysql2', '< 0.3'

Rebuild the MySQL adapter with your version of MySQL.

gem uninstall mysql2
gem install mysql2 -- --with-mysql-config="C:/wamp/bin/mysql/mysql5.5.16/bin/mysql_config.pl"

Download the MySQL connector from the MySQL website.

http://dev.mysql.com/get/Downloads/Connector-C/mysql-connector-c-noinstall-6.0.2-win32.zip/from/pick

Once you have the zip file, uncompress it and copy lib/libmysql.dll to the Ruby bin directory. Finally, go back to your Rails project and run the following command.

bundle install

I was finally then able to run a command such as the following.

rails generate model Something name:string

Without it throwing any errors up.

Atomic commits

Thursday, August 9th, 2012 | Limited, Programming

If you’re using version control (you are using version control, right?), the question sometimes comes up as to how often you should commit.

I’m a commit fanatic, I like to commit as soon as soon as I can, no matter how small the change. Other people prefer to wait a little and make large commits all at once. However often you choose to commit, you should make sure it falls within the constraint of being atomic.

This means that each commit is one single, complete change. That is to say that you shouldn’t commit multiple changes at once, nor should you commit a change that is only half done. This is scope in this for different approaches of course “one single, complete change” could be part of a several step refactor as long as the changes stand on their own, so could committing the refactor at once if it is all part of the same unit of work. But the important thing either way is to try and stick to atomic commits.

Think of it like an ACID transaction. Presumably you’re system works before you make the commit and it should work after too. It should contain just one feature so that if you want to roll that specific feature back, you can roll back that one commit without rolling anything else back.

With Git, where people may want to take out of sequence commits, this becomes especially important. People may want to take one of your changes, but not the other, so making sure two changes are in two separate commits is very important. Generally, if your descriptive commit comment (you are writing descriptive comments, right?) contains the word and, you probably need to re-think your commit.