Adam Elliot

Jun 17 2009
Comments (View)
May 29 2009
Comments (View)
May 15 2009

Delegate Binding in #MacRuby

MacRuby for the most part is straight forward. You see the Objective-C code and it’s pretty clear what the Ruby alternative is. This isn’t quite the case with multi-parameter delegates.

Let’s say you are using a QTCaptureView and want to use the view:willDisplayImage: delegate method. Here’s how you’d define it it MacRuby:

def awakeFromNib
  captureView.setDelegate self
end

def view(v, willDisplayImage:image)
  # Do something
end

Surprise (or at least I was surprised), this is almost exactly the same as the Objective-C version. After doing a little more digging I found that this is a wonderful product of Ruby 1.9 and it’s new way to declare Hashes.

h = {a: 1, b: 2}

This works just like JavaScript, which rocks. It’s always nice when the solution you are looking for is staring you right in the face (and it simpler than you expected).

-Adam

Comments (View)
May 14 2009
Metatweeting Robosexual fantasies @Paul_c: - Posted using mobypicture.com/?42ct4c

Metatweeting Robosexual fantasies @Paul_c: - Posted using mobypicture.com/?42ct4c

Comments (View)
+
Comments (View)
+

Railsconf Wrap-Up Part 1

I got to attend railsconf last week and there was a tonne of information that I attempted to absorb, so I figured I’d try to put it all together in a useful fashion, and maybe I will have a better chance of retaining it. Much of the information presented there was posted on the web else where or right on the site itself, so this can act as a list of resources to any information I can link to.

First off all the keynotes and some of the sessions were video tapped. You can check them all out at the Railsconf blit.tv page.

I’ve tried group the topics together logically; I’m going to start with performance optimization:

Performance & Optimization

There’s been many ways over the years on how to run rails over the years. We’ve gone from webrick to mongrel & lighttpd then to apache and passenger.

For Ruby in a production enviroment Ruby Enterprise Edition is the way to go and it sounds like it will work with Ruby 1.9 in the near future as well.

Ruby Enterprise Edition: http://www.rubyenterpriseedition.com/download.html

 mkdir ruby-enterprise
 cd ruby-enterprise
 wget http://rubyforge.org/frs/download.php/55511/ruby-enterprise-1.8.6-20090421.tar.gz
 tar xzvf ruby-enterprise-1.8.6-20090421.tar.gz
 sudo ./ruby-enterprise-1.8.6-20090421/installer

Then just follow the instructions in the terminal

The best options for hosting your rails app is to use Phusion Passenger and Nginx with Enterprise rails. Nginx is the blazingly fast Russian webserver and Passenger added Nginx support in 2.2.

Comparisons: http://www.pcdoctor-community.com/blog/posts/2008/05/15/Nginx-vs.-Apache2-in-Rails-Running-Death-Match/ http://blog.webfaction.com/a-little-holiday-present

Getting going with Nginx on Ubuntu:

If you ran the ruby enterprise edition installer the you can just do a:

 sudo /opt/ruby-enterprise-1.8.6-20090421/bin/passenger-install-nginx-module

And follow the instructions. Getting Nginx all setup is a bit more work and there are plenty of good tutorials on that. Here’s just one: http://github.com/jnstq/rails-nginx-passenger-ubuntu/tree/master

Performance

The Advanced Performance Optimization of Rails Applications talk by Alexander Dymo offered many great ideas and techniques for making your rails applications run as quickly as possible. His slides offer many good tips and can be found here. I’ll go over some of the gems here for a quick reference.

  • The buildin date object is very slow use the (Date::Performance)[http://tomayko.com/src/date-performance/]
  • Use String::<< instead of String::+=
  • Avoid BigDecimal comparisons with strings and integers
  • Ruby 1.9.1 and JRuby are much faster than current Ruby 1.8.6

Measure your running code, find what’s going slow and optimize that. Tools to use * Linux - strace & oprofile * Mac - dtrace

Check your Javascript speed. The server does log requests it doesn’t get because of a slow client. To test for full site optimization use Internet Explorer. If it runs well there it runs very well everywhere else. IE Has a profiler and can switch between IE 7 & 8.

The Ruby Profiler can tell you where your code is running slow:

 gem install ruby-prof

Alright that’s all for now.

-Adam

Comments (View)
May 12 2009

Nokogiri on Ubuntu

We use Nokogiri on our production servers and when I went to install it with the standard rake gems:install it fails. The error that is reported states that you need libxml2 installed. This isn’t quite correct. You will need libxml2-dev and libxslt1-dev. So to get everything up and running:

sudo apt-get install libxml2-dev libxslt1-dev
sudo gem install nokogiri
Comments (View)
+

Check Sites

As a web developer I have several sites I need to manage. It’s generally bad when one of these sites goes down, but it does inevitably happen. A power failure may happen, a code change could break something, but not right at deploy, or something completely different. The real shitty part is when your customer is the one to tell you your site is down instead of you finding out at the point of failure.

With rails sites there are great tools that will monitor errors and exceptions on your site, but if your rails app fails to even start your stuck in the dark. To get around this problem I found a script by Brian Wigginton that checks sites in a file and sends emails when the response code isn’t a 200 or 300 level response code.

By default it looks for a file called ~/.check_sites which should look like:

http://adamelliot.com adam@warptube.com,support@warptube.com
http://warptube.com adam@warptube.com

And to set it up in cron to check every 15 minutes do something like:

*/15 * * * * ~/bin/check-sites.rb

And the code:

-Adam

Comments (View)
May 11 2009
Comments (View)
+
Comments (View)
Page 1 of 2