Quote of the day

“Perl – The only language that looks the same before and after RSA encryption.” – Keith Bostic

Posted in Quote | Leave a comment

Rails mentoring

A great way to learn is by teaching others. Why? Because explaining solutions to others gives you a greater understanding of the problem and the solution itself. I have never had any opportunities to really mentor anyone before. Recently, I signed up to RailsMentors site as a mentor and few weeks after that I actually got a chance to help someone with their Rails issues. It has given me a good understanding of someone else’s approach to Rails and the places where they usually get stuck. It’s not just mentoring, you always find some nice tricks someone has used.

For instance, I helped him set up CanCan in the app and advised about using TDD (with Rspec and Cucumber). In addition, I suggested using faker along with factory_girl to seed the database and helped them with routing issues.

Learning is not just one way. I found this cool way in javascript to load and use a font from his code and it was fun and satisfying helping someone out with their issues. Here’s a proof of my performance.

Posted in Programming, Rails | Tagged , | Leave a comment

Apache on Mac

To restart apache on Mac, all you need to do is uncheck(and check) Web Sharing through your System Preferences -> Internet and Network -> Sharing but what it does not tell you is any error messages in the config. Therefore, it would look like that the apache has restarted but the server would still not work. If that is the case, just run:

/usr/sbin/httpd

in the Terminal and it will spit out any errors.

Posted in Programming | Tagged , | Leave a comment

Moving from Dreamhost to Linode

Yes, after 3 years with DreamHost, I have finally moved my blog to a Linode server and it did not go as planned therefore, lack of activity on the blog. I never had any issues with it. It was cheap, gave me easy options to install WordPress and all. But being a programmer, I wanted to get the Server Admin experience under my belt and hence the move to a Linode server. I bought it quite a while ago but since I was in the middle of the DreamHost contract (bought a 3 year one), I left it running on it. Unlike my experience, several bloggers and developers had issues with DreamHost. You can read about it here and here. Let us be honest that it’s good value for money.

It’s not until you have actually moved that you realise the advantages of a hosting service. Things like mail server, I tried postfix and had numerous issues. But, thanks to Google apps standard edition. E-mail works like a charm now. Setting up php with nginx and fast-cgi required a few hours and if you are not a developer or don’t have the ambition to learn these things, DreamHost is your saviour.

If you ever decide to move from DreamHost to Linode, please note that DreamHost offers 60 days grace period for you to pay your bill or move your sites. I did not know it and had to move all the sites in a rush where I could have planned it and since DreamHost does not offer a static IP once you change domain’s nameservers, you can’t even access the MySQL database.

Also, remember one thing, no matter what never install a CentOS linux distro on your Linode. Tell you why in my next post.

Posted in Programming, hosting | Tagged , , , , , | 1 Comment

Using cucumber-0.9.2 and cucumber-rails-0.3.2 with I18n

I have previously, blogged about using factory_girl with clearance and this post (quite similar to that one) describes the need to use messages (notice, success or failure) in your locale file with cucumber and how to go about using them. Needless to say, this pertains only to the Rails crowd but I guess anyone can set-up their gems or ruby aps to extract similar behaviour. I have been using cucumber with Rails3 and been blogging about my experiences for last couple of weeks.

Imagine, you are writing a feature and you would like to test that the correct flash message gets displayed when something goes wrong or as a notification to the user. In order to make sure, I use the same message across the board I would like to add them to my locale file and read it from there. Instead of having to copy paste them since with cucumber outward-in TDD approach one writes the feature then jumps into write controller and model specs to make the feature pass. Now, in order to achieve that all you gotta do is load the i18n library when the cucumber features are run. So, just add the following to env.rb:

require "i18n"

But, I can’t use the following snippet in my scenarios. How will I use it with cucumber?

I18n.translate :invalid, :scope => [:login, :errors, :messages]

Simple, just use it in your step definitions. Instead of the following scenario:

Scenario: Logging in
  Given I am on sign in page
  When I press "Log in"
  Then I should see error messages

use this scenario:

Scenario: Logging in
  Given I am on sign in page
  When I press "Log in"
  Then I should see error messages

In your step definitions, add a new step for “I should see login failure messages” like this:

Then /^I should see login failure messages$/ do
   Then %{I should see "#{I18n.translate :invalid, :scope => [:login, :errors, :messages]}"}
end

And that’s it. It does sound like a lot of work but it took me under 15 minutes to get it working.

Posted in Programming | Tagged , , | 1 Comment

Rails3 and rspec-rails-2.0.0.beta20 and cucumber-rails-0.3.2

Last week, I started a Rails3 project and considering I am sucker for TDD. I added the current stable versions of rspec-rails and cucumber-rails to the Gemfile. Now, this is a simple project (and honestly speaking I could have just used Sinatra) for the same, but I wanted to get a Rails3 app up and running.

When I say simple, I mean no “ActiveRecord”. Some might say I am better off using Sinatra with so little codebase to load but like I said it was just to get some hands on experience with Rails3. Developers mostly use rspec-rails and cucumber-rails with activerecord and since I wasn’t going to use activerecord, I hit a few road blocks.

Rspec-rails
First, let’s have a look at changes one needs to do use rspec-rails with activerecord. I am assuming that you have installed rspec in your rails app using the new rails3 generators and you have a spec_helper.rb in your spec folder. Just comment out the following lines to make it work with activerecord:

  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true

spec_helper.rb file does have directions to remove these files in the absence of activerecord.

Cucumber-rails
Similarly, cucumber-rails actually relies on the fact that activerecord is always used. When you run rake:cucumber, you will probably see the following errors:

** Invoke cucumber:all (first_time)
** Invoke cucumber:ok (first_time)
rake aborted!
Don't know how to build task 'db:test:prepare'

Reason, because cucumber depends upon the db:test:prepare task to run. But in the absence of activerecord this causes issues since there is no db. I added a patch for the same which checks for the presence of database.yml and takes action accordingly. Here’s the lighthouse ticket and patch. You can monkey patch it in your app by modifying the rake task in lib/tasks/cucumber.rake.

In the end it was a good experience and taught me a bit more about cucumber and rspec.

Posted in Programming | Tagged , , | Leave a comment

Ruby 1.9.2 and RVM

RVM is no doubt an amazing piece of code. It just makes life so much easier. Ruby 1.9.2 is out. Woo-hoo! and I tried installing it with rvm only to encounter an error. I fixed the error and this post outlines my shenanigans leading to an easy solution.

Mark has done a nice post on rvm with ruby 1.9.2 and rails3. I used it as my starting point and ran the following command in the Terminal.

rvm install 1.9.2 -C --with-readline-dir=/opt/local,--build=x86_64-apple-darwin10

It downloads the ruby 1.9.2 and errors whilst running make. Here’s the rvm error message:

info: Installing Ruby from source to: /Users/andhapp/.rvm/rubies/ruby-1.9.2-p0

info: /Users/andhapp/.rvm/src/ruby-1.9.2-p0 has already been extracted.

info: Configuring ruby-1.9.2-p0, this may take a while depending on your cpu(s)...

info: Compiling ruby-1.9.2-p0, this may take a while depending on your cpu(s)...

error: Error running 'make ', please check /Users/andhapp/.rvm/log/ruby-1.9.2-p0/make*.log

error: There has been an error while running make. Aborting the installation.

This points at a make error log. Further investigation of the make.error.log file revealed the following error message (only the last 10 lines):

_sdbm.c: In function ‘chkpage’:
_sdbm.c:864: warning: comparison between signed and unsigned
raddrinfo.c: In function ‘nogvl_getaddrinfo’:
raddrinfo.c:140: warning: passing argument 3 of ‘ruby_getaddrinfo__darwin’ discards qualifiers from pointer target type
bytecode.re: In function ‘sycklex_bytecode_utf8’:
bytecode.re:442: warning: implicit conversion shortens 64-bit value into a 32-bit value
rubyext.c: In function ‘mktime_do’:
rubyext.c:278: warning: comparison between signed and unsigned
/Library/Ruby/Gems/1.8/gems/rdoc-2.4.3/lib/rdoc/rdoc.rb:51: warning: already initialized constant GENERATORS
[BUG] cross-thread violation on rb_gc()
ruby 1.8.6 (2009-06-08) [universal-darwin9.0]

make: *** [rdoc] Abort trap

There’s a very strange thing in this error message. For some reason, it is referencing ruby 1.8.6. But, why? Well, I am not sure what causes it but here’s how I fixed it. My default system ruby is version 1.8.6 and that was the current ruby in the context when I ran the rvm command. I used rvm to change the current ruby to 1.9.2-preview1 and then re-ran the command. Volia! It worked.

No idea what causes it but worth investigating!

Posted in Programming | Tagged , , | 2 Comments

Reading Open Source Code

I was very confused the first time I heard this phrase – “Read Open source code”. I could not really grasp the concept of reading someone else’s code. How can that improve your own programming skills? But, I can safely say that it does help. Two things that I found out recently, both reading some open source code. Here they are:

1. I found this trick used in Paperclip’s code. rescue is basically to catch the exceptions and do the needful but one can use them inline. Let us look at an example:

# Usual Way
begin
  1/0
rescue => e
  puts "Exception"
end

# Cool way
1/0 rescue "exception"

We all know what the first approach does. The second one is quite simple as well. Execute the code followed by rescue if there is an exception in the code before rescue. Now, I will agree that both approach has it merits but still the second one is much cooler and neater.

2. Passenger is an amazing, cutting-edge library that I usually read through and there is something very interesting I found in the code. There is a library in ruby called Etc that allows one to query stuff like the current user and so on. I had no idea one could do it.

Well, every programmer should read other’s code and his own code from time to time to see what he could improve and where we went wrong.

Posted in Programming | Tagged , | Leave a comment

Bundler 1.0.0.beta.5 and Rails 2.3.8

I wrote about Bundler a couple of months ago. With newer versions of bundler out two of my issues at that time, using engines and issues with time_zone have been resolved. I am currently using bundler 1.0.0.beta.5 with a rails 2.3.8 application. I know I should just move it to Rails3 and yes that is in the pipeline and hopefully very soon.

Posted in Programming | Tagged , | Leave a comment

Publisha: the one stop publishing platform

Just started another blog to talk about programming related stuff using Publisha. And the best thing is it will be on iPhone straight-away. I don’t have to even move my finger.

Cool.

Posted in General | Tagged | Leave a comment