>FreeBSD + VirtualBox + RoR = nice, easy development environment :-D


So the new St. Noble security intern, Matt, has a hard-on for FreeBSD and has been trying to convince me to use it. Though I like to give him flak about it, I do actually enjoy the simplicity that it has to offer, and the delightful lack of a GUI – call me old-fashioned 🙂 [no, really, I do actually love a nice simple text interface, especially when I’m coding. Visual Studio, Eclipse, all those IDE’s are nice and all with their integrated file management, library browsers and auto-completion modules but when I code rails, I like using a simple text editor with nothing but the most basic syntax highlighting]  
I’ve found that, amongst other things, FreeBSD is great to use as a lightweight Ruby on Rails development environment, especially when combined with VirtualBox. If you own an IronKey, you can set up PortableVirtualBox and get it up and running quite easily.
Anyway, this is a simple guide to setting FreeBSD as your development environment. It’s not very in-depth, but I hope it will provide you enough tips and tricks to set yourself up without committing harakiri or throwing your machine out the window…
When you’re asked to do the partitioning, start by using auto-partition; take note of the different sizes, then adjust your swap space so that you can do a bit more swapping – I would recommend 1 GB instead of the de-facto standard. Choose a development installation on FreeBSD, and install the ports collection. Apart from that, follow the wizard. Pretty simple, huh?
I would recommend setting up sudo, but that’s not 100% necessary since your box is effectively sectioned off from all other computers. If you want to do that, then run ‘pkg_add -r sudo’. Remember to edit your /etc/sudoers file and add your regular username, and optionally prevent root from logging in via SSH by editing your /etc/ssh/sshd_config file.
VirtualBox setup:

Use a basic FreeBSD VM template, with two virtual NIC’s: set your first NIC up to be on the host-only network, and your second NIC to be on the NAT network. This allows you (and only you) to connect to your dev environment via SSH or web whilst providing the VM with a means to access the ‘net (especially practical if you’re consuming web services).

SSH and bash

By default, freeBSD uses csh. I’ve tried using tcsh instead, doesn’t seem to work for me; bash, on the other hand, works fine.  To install that, execute ‘pkg_add -r bash’ and wait until it’s installed. You can run it manually thereafter by executing ‘bash’ whether you’re at the console or remotely logged in via SSH. Ideally, though, you’d probably want your shell to be bash as soon as you start, right? Here’s the command to do that: ‘chsh -s /usr/local/bin/bash username‘, where /usr/local/bin/bash is the path to bash (so before executing the command, be sure to run ‘which bash’ to double-check the path) and username is your actual username. You must then edit your /etc/passwd file and substitute your shell for /usr/local/bin/bash – it’s the last field on the line with your username on it. You must both execute the chsh command and edit the passwd file in order for the switch to work! Kudos to vivek for the nice, easy tutorial that I didn’t read completely the first time like an idiot. If you follow it to the letter, this will work for your console, SSH, and will allow you to use SCP (there’s another way to set up bash on SSH, and that’s to use the ForceCommand directive in sshd_config – but that messes with your ability to use SCP).

Ruby, Rubygems, and Rails

No way around it – you’ve gotta compile the sucker; everything you need should be accessible via http://rubyonrails.org/download. Shouldn’t be too hard though: uncompress the tarball, run the usual ‘./configure; make; make install’. Download the rubygems tarball and install it using ‘ruby setup.rb’. Finally, run ‘gem install rails’.

Nano: syntax highlighting

A final note: if you are a fan of nano like me and you would like syntax highlighting, you can create a .nanorc file in your home folder and use the following example as a starting point: http://code.google.com/p/nanosyntax/source/browse/trunk/syntax-nanorc/ruby.nanorc

>A (very) simple ruby script to add files to your repository

>You’re developing a ruby on rails site and, as a dutiful developer, you’ve set up versioning on your code. As you probably already know, when you generate a new object there are quite a few files that get created — so every time you want add a new model, you have to type that “svn add [filename]” command… A bit boring if you ask me.

In Windows, all you really need to do is use a graphic interface like TortoiseSVN; it’s a bit clunky at times but it definitely does the trick. In Mac OS X and Linux, the most powerful and flexible tools are already at your disposal in your command line — so why go through the fuss of a GUI?

I’m sure there are plenty of elegant, easy solutions to this — but here’s mine:

  • Open up your favorite text editor
  • Copy &Paste this code into it:


delim = “? “

results = `svn stat | grep #{ARGV[0]}`.split “n”

results.each do |result|

`svn add #{result.gsub delim, “”}`


  • Save the file in, say, the same directory as the project that you have under SVN (don’t save it in the project directory — that wouldn’t be clean). I call mine svnaddfiles.rb
  • For convenience, make that file executable with a “chmod u+x svnaddfiles.rb”
  • From your project directory, you can use it like this: “../svnaddfiles.rb [file prefix]”

Voilà — easy adding of many files, using just ruby, svn and grep!

>Unicode in Ruby with SQL Server

>I’ve been using Ruby on Rails a lot recently, and in one instance I found myself in a bit of a pickle: using UTF-8 fields with SQL Server 2005. After a bit of googling and a lot of playing around, I finally found a solution that seems to work; figured that someone out there might find it useful…

Here’s the environment in which I tried this out:

Here are a few things I tried beforehand and which didn’t work:

  • Installed mssqlclient (had high hopes for this one but I kept getting errors about the adapter not existing. If anyone’s got some insight here, please let me know and I’ll add your URL or text to the article :-D)
  • Tried altering the column types (biggest problem here was with the constraint. Yes, you can drop the constraint but I found that it beat the purpose of using rails’ scripts to generate the data model because every time you’d want to drop the column you’d have to edit your migration and plug in the constraint’s unique name… Yuck.)

You’ll also note that, according to rubyonrails’ wiki on using unicode strings (http://wiki.rubyonrails.org/rails/pages/HowToUseUnicodeStrings), you don’t need to change much of your application’s code to switch to UTF8. As a matter of fact, I simply performed the necessary tests from script/console and realized that $KCODE and ActiveSupport:MultiByte were already set / installed.

My problem actually occured when my migrate scripts were generating the data model: instead of generating the utf8 fields nvarchar and ntext, it was creating varchar and text. If your environment is already set up to support utf8, the only thing you need to do is modify the sqlserver_adapter to generate the correct fields.

This is how it’s done:

  • Navigate to C:rubylibrubygems1.8gemsactiverecord-sqlserver-adapter- (assuming that you’re using the default installation paths)
  • Save a copy of sqlserver_adapter.rb (duh :-D)
  • Edit the sqlserver_adapter.rb file and locate the native_database_types method.
  • Change ‘varchar’ to ‘nvarchar’ and ‘text’ to ‘ntext’ – Save the file
  • Regenerate your data model (yup, you’ll need to delete your existing database). If you’ve already got production data in your database, you’re most certainly going to want to backup your data, restore it to another database and migrate data using ETL’s (hand-rolled or SSIS…) Best of luck!

A few last things:

  • In your database.yml file, make sure you’ve got this line in the profile:

encoding: utf8

  • In the ApplicationController class, you’ll need to define a before_filter method to set the correct headers; open up the application.rb file and set up a before filter like this:

before_filter :set_charset

def set_charset

#Sets the response headers to UTF-8. Alone, this doesn’t do squat: 

response.headers[“Content-Type”] ) “text/html; charset=utf-8”

#Sets the code page; very important…

WIN32OLE.codepage = WIN32OLE::CP_UTF8


Many thanks to Andres Mondano Pellegrini for the code. You’ll find his article, which saved my ass, here: www.gaugeus.com (if you’re having trouble finding it, perform a search on “Unicode Support in Ruby on Rails with MySQL and Microsoft SQL Server”)