Art Of Programming

musings by Dmytrii Nagirniak

Setting Up Ubuntu 11.04 Server for Rails With RVM

I had to set up the server (Ubuntu 11.04) for running couple of Rails apps and thought I could share the notes I’ve written while doing that. Any feedback and improvements are welcome.

apt-get update ; apt-get upgrade

# set timezone
dpkg-reconfigure tzdata

bash < <(curl -s

# update /etc/bash.bashrc

# -y to answer YES for questions
aptitude -y install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev

rvm install 1.9.2; rvm use 1.9.2 --default

# while ruby is being installed prepare nginx
cd /usr/src
tar -xf nginx-1.0.0.tar.gz
cd nginx-1.0.0

# Use one place for installation
rm -r /opt/ && ln -fs /usr/local /opt

gem update --system ; gem update
gem install bundler
#install imagemagic
apt-get -y install imagemagick

# proceed with passenger
apt-get -y install libcurl4-openssl-dev
gem install passenger
# additional otions:
# --prefix=/opt/nginx
# --with-http_gzip_static_module --with-http_stub_status_module --with-http_sub_module

# add nginx autostart script:
# verify the correct paths
cd /opt && \
wget -O && \
mv /opt/ /etc/init.d/nginx && \
chmod +x /etc/init.d/nginx && \
/usr/sbin/update-rc.d -f nginx defaults

# add to /opt/nginx/conf/nginx.conf
include /home/*/apps/*/current/nginx.conf;
/etc/init.d/nginx restart

# copy ssh keys (from local computer)
cat ~/.ssh/ | ssh "mkdir ~/.ssh ; cat - >> ~/.ssh/authorized_keys"

# change default ssh port from 22 to something to get rid of nasty attacks
vim /etc/ssh/sshd_config # change the port number
service ssh reload

#install firewall?? No need for now as only openning 80,443 ports
# apt-get install ufw

That should be enough for you to start using the server. In the future post I plan to describe my setup to make deploying new applications easier.


I've used this guide for 3 apps and it's worked great. However the step "use one place for installation" wipes everything out for some reason so I skip that part but otherwise thanks.

Unix - Do Not Reinvent the Wheel

One of the most interesting videos I watched from the Scottish RubyConf 2010 is Unix: Rediscovering the wheel. If you use Ruby/Rails and POSIX systems you gotta know those things. This is just a quick list of notes to keep it mind. So you will have to Google if something catches your attention.

Scheduling tasks with Cron
- define all tasks in cron
- don't make Ruby do scheduling

Background processing with ATD daemon
echo "convert image.jpg image.png" | batch

Logging with syslog
- don't use rails log

Resource limiting with rlimit
ulimit -v 102400
ruby -e "' ' * 1024 * 1024 * 1024 *1"
failed to allocate memory (NoMemoryError)

CPU and Disk IO priorities
nice -n 19 rake xapian:update # run rake with priority 19
ionice -c3 rake xapian:update # same for disk IO

Atomic renames
- don't use locks
- write to temp then rename

Secure self-cleaning temporary files
- create temp fie
- delete it while holding it open
- write/read securely

Page cache expiry with find
find public/cache -type f --mmin +10 -delete # del all files 10 mins old in a "transaction"

Log rotation with logrotate
- Rails sucks at it, use time proven and tested UNIX

Managing daemons with start-stop-daemon
- don't reinvent the wheel starting/stoping daemons (mogrels etc)
    start-stop-daemon -d $railsdir -b -o -p $pidfile --start --starts rake xapian:update
- run cron tasks using start-stop-daemon to avoid race condition (it will exit if process already runs)

Real time signals
use it in ruby:
trap("USR1") { reopen_xapian_db }

Raliable messaging with... SMTP!
Due to atomic and reliable delivery it can be use as a message queue.
Can replace RabbitHQ etc

UNIX based Messaging system

Self-organising systems with AVAHI

Modularised config files

Watchdog daemon

What I’ve Learned From Scottish RubyConf

I watched the videos from the Scottish RubyConf 2010 and thought I could share some interesting moments as simple notes. So there you go:

Harmony for browser testing

Capybara.driver = :rack_test # uses Harmony and can run JS

Good talk about Arel: 1LT_04-mp4_500mbs.mp4

Rack GeoIP.
Rack GoogleAnalytics
Rack::Cache (standard)

Server side include to render page without parts of content that fail
(Nginx + Rack)

EvenMachine:: WebSockets add-on uses JS like API.
Other options:
-cramp (higher level websocket)

Initialise multiple variables in JS:
var a = 1, b = 2, noInitialised;
jQuery has "end" method to go up in the chain.

:copen - opens the console output
f - find next occurence of char in this line
dap - delete paragraph
cap - change
yap - yank
ytCHAR - yank from here to next CHAR
Ctrl-v - visual block mode
:split, :sp, :vspilt, :vsp - split into 2 wins
ls - list buffers touched
q1, q2, q3... - record macro into register
@1, @2, @3... - play the macro from register

:R - Rails related file
:A - Rails alternate file
:Rextract - Rails partial extract
Shift-k - integrated Ruby ri

figutive.vim - more extensions: Glog, Gblame, Gstatus, Gdiff, Ggrep etc
nerdtree.vim - files tree view
unimpaired.vim - auto-brackets
Hashrocket configured Vim at Github hashrocket/dotmatrix
Vimium, Vimperator - Vim in browser
Remotely connect and pair in Vim

UTF-8 is backwards compatible with ASCII
Ruby doesn't know about UTF-16 - it's messy

Globalization TODO:
- Do same as .NET CultureInfo
- Do different ways to localize different kinds of langs

You can generate UML model of Rails model using:
railroad -M | dot Tpng > model.png
uml dumper

- rake stats # LOCs
- FLOG Weighting the code AST - Assignment Branch Condition (ABC)
- FLAY - code similarities
- Saikuro - cyclomatic complexity
- Roodi - design issues
- Reek - design issues and specific smells
- Churn - changes to file with Git
- Rcov - test coverage
- Heckle - test coverage with code mutation
- metric_fu

You can also grab the Gist. Hope you find some moment useful.

Unit Test Actions With ValueProviderFactories in ASP.NET MVC3

The problem: In the process of upgrading from MVC2 to MVC3 we have faced an issue where some of the ValueProviders gain explicit access to static HttpContext thus making it too hard to test complex actions.

The solution was not obvious at first and I want to provide enough context before the solution. The way I test the controllers is this:



public class CurrentUserControllerTest : BaseTestFixture {
    private CurrentUserController controller;
    private Mock<ICurrentUserService> userService;
    private FakeHttpContext http;
    protected override void Init() {
        userService = new Mock<ICurrentUserService>();
        controller = new CurrentUserController(Services.Account, userService.Object)
            .FakeContext(context => http = context);
    public void CanSeeSecuritySettingsPage() {
        userService.Setup(x => x.ViewUser()).Returns(new User {SecurityQuestion = "sq"});
    public void SuccesfulUpdateRedirects() {
            .ShouldRedirectTo(action: "Index", controller: "Dashboard");

The most relevant piece of code here is the extension method FakeContext. It ensures the controller is not going to access real HttpContext. The main difference between MVC2 and 3 is that the latter uses HttpContext in FormValueProviderFactory and QueryStringValueProviderFactory. So we need to get rid of it. And of course we don’t want to modify all our tests and complicate them providing explicitly FormCollection. Additionally we definitely do not want to go into trouble instantiating HttpContext.

Fortunately MVC is extensible enough and has number of hook that we can use. In this case we need to replace those two bustards (FormValueProviderFactory and QueryStringValueProviderFactory). Which can be done with this simple extension method:

public static class ValueProviderFactoresExtensions {
    public static ValueProviderFactoryCollection ReplaceWith<TOriginal>(this ValueProviderFactoryCollection factories, Func<ControllerContext, NameValueCollection> sourceAccessor) {
        var original = factories.FirstOrDefault(x => typeof(TOriginal) == x.GetType());
        if (original != null) {
            var index = factories.IndexOf(original);
            factories[index] = new TestValueProviderFactory(sourceAccessor);
        return factories;
    class TestValueProviderFactory : ValueProviderFactory {
        private readonly Func<ControllerContext, NameValueCollection> sourceAccessor;
        public TestValueProviderFactory(Func<ControllerContext, NameValueCollection> sourceAccessor) {
            this.sourceAccessor = sourceAccessor;
        public override IValueProvider GetValueProvider(ControllerContext controllerContext) {
            return new NameValueCollectionValueProvider(sourceAccessor(controllerContext), CultureInfo.CurrentCulture);


And the final implementation of FakeContext extension that I use (you will need to trim it to your particular case) is:


public static TController FakeContext<TController>(this TController controller, Action<FakeHttpContext> exposeAction = null) where TController: Controller {
    ApplicationMetaData.RegisterAll(); // Additionally register all ModelBinder so tests behave is in production
    if (ViewEngines.Engines.Where(x => x is SparkViewEngine).Empty()) {
        // We only use Spar view engine that can render view during testing
        ViewEngines.Engines.Insert(0, GetTestViewFactory());
    // That is the entry to all the fakes, implementation is trivial so not here
    var context = new FakeHttpContext();
    controller.ControllerContext = new ControllerContext(context, new RouteData(), controller);
    controller.Url = new UrlHelper(new RequestContext(context, new RouteData()));
    // And finally, here we ensure no ValueProviders access HttpContext
        .ReplaceWith<FormValueProviderFactory>(ctx => ctx.HttpContext.Request.Form))
        .ReplaceWith<QueryStringValueProviderFactory>(ctx => ctx.HttpContext.Request.QueryString));
    if (exposeAction != null)
    return controller;


Hope that helps. Have fun!


Dmytrii Nagirniak
I can't remember already, but there are couple of the ValueProviders that you need to replace. Just look at the exceptions you get. Should be pretty straight forward. Worked for all other people.
duzn't work

Screencasts for This Weekend

This weekend I was watching the screencasts instead of Sunday movies. It left me with a lot of inspiration and impressions. So I thought I would share the stuff you might also be interested in:

  1. ASP.NET MVC vs Ruby on Rails - The .NET Rocks Smackdown
  2. Why Ruby?
  3. Testing C# and ASP.NET Applications with Ruby
  4. Ruby for .NET developers (Scott Belware is awesome BTW)
  5. Riding IronRuby On Rails


Other things are:

  1. MonoTouch DeepDive
  2. Meet Rails 3 by PeepCode (a producer of high-quality screencasts)

I think if you are a software engineer with open mind then you do need to watch some of the videos. This is especially true if you are a .NET developer who wants to explore other areas and technologies.

Stuff Learned Today - Ruby and Rails

  1. Ruby ignores underscores in numbers, so you can write 53782156 as 53_782_156 which is much nicer.
  2. Suggested localisation for Formtastic’s collections. Not sure whether it is important as it is still pretty easy to workaround.
  3. To use all the ActiveSupport (v3), do require ‘active_support/all’ (note the underscore!).
  4. We can add methods to associations by opening a block and (but scopes are much cleaner on the other end of association class).
  5. Testing “access denied” and “requires user” is easier with custom matchers.
  6. Rails URL helpers are not available in custom matchers for some reason.
  7. Shoulda’s RSpec matcher validates_presence_of does not work with non-persistent models.
  8. Rails params hash has string keys, not symbols. Got issues with it when relying on keys being symbols.

Rules of Productivity

How do we get more work done? It is a question that every manager and every passionate worker faces. Yet, for the most part, teams operate on gut instinct and habit. The results are less than optimal. [source]

What is productivity?

  • + Work accomplished
  • - Work required to fix defects
  • - Work required to fix bad design decisions

Productivity can be negative when there is more harm than good.



Experiment 1: Overtime

Working more than  40 hours a week leads to decreased productivity.

  • < 40 hours/week – working not “enough”.
  • > 60 hours/week – small productivity boost.

The boost only lasts for 3 to 4 weeks and then turns negative. Though can be used for a short-time productivity increase.

So, work 40 hours a week with time for rest and family. Never work 2 months of 60 hrs/week. Overall productivity will be lower, despite the initial boost.

Experiment 2: Work harder in bursts.

  • Take advantage of the burst during overtime.
  • Crunch for a week and work only 40 hrs on another week.
  • Any more efficient work patterns?

Anything over 40 hrs/week results in a recovery period, no matter how you split it up. 40 hrs/week can be 10 hrs/day for 4 days and one day off. This pattern of work can increase productivity by 10-70%.image


  • Raise productivity immediately with short spurts < 3 weeks.
  • Overtime temporarily to meet deadlines.
  • Plan for the reduction in productivity immediately afterwards.
  • Consider 4-day work week as a flexitime option.


Experiment 3:  Performance for knowledge workers declines after 35 hrs, not 40.

  • Creativity and problem solving decreases faster with fatigue than manual labour.
  • Grinding  out problems by working longer  on average result in inferior solutions.
  • Lack of sleep is particularly damaging.


  • Overtime kills creativity.
  • If you are stuck on a problem, go home or take a break.
  • Get 8 hrs of sleep to improve your problem solving abilities.


Experiment 4: Teams on overtime feel like they are doing more, but actually accomplish less.

  • Team with overtime feels like they are doing much more than the team without overtime.
  • Team without overtime produces the better product and accomplishes more.


Humans ignore the systematic costs and physiological biases:

  • Failure to measure (cost of defects, bad design decisions, missed opportunities).
  • Linear extrapolation (initial productivity boost is seen as same in the future).
  • Habit (a bad one!).
  • Self reported excellence (behaviour rewarded independent of the results).


  • The feeling of increased long-term productivity is FALSE.
  • Use customer metrics to determine the productivity.


Experiment 5: Productivity is maximised in small teams of 4-8 people.

  • Productivity of a small group is 30-50% higher than groups over 10 people.
  • Too high cost of communication for groups with > 10 people.
  • Smaller teams don’t have enough breadth to solve a wide array of problems.


  • Split your teams into cross-functional teams.
  • Link small teams using ‘”scrum-of-scrums”.
  • Create processes for:
    • growing new teams;
    • splitting large teams;
    • transitioning to new projects;


Experiment 6: Seat people on the same team together in a closed team room.

  • 100% increase in productivity.
  • Faster communication and problem-solving.
  • Fewer external interruptions to the team increase overall productivity.


  • Seat the team in their own room. With walls.
  • Give at least 5 sqr/meters per person.
  • Create side rooms for private conversations, phone calls, meetings.
  • Minimise non-team distractions.


Experiment 6: Cross-functional teams outperform siloed teams

  • Produced more effective solutions in the same time.
  • Much more likely to generate breakthrough solutions.
  • Short-term loss of negotiation of norms.

This is why:

  • Fewer external dependencies mean fewer lengthy blockages.
  • Team has the breadth to see the forest, not just the trees.
  • Different perspectives mean lower chance of groupthink.


  • Create the team where every skill needed to solve the problem at hand is in the same room.
  • Limit the charter: “Do everything = big team”.
  • Fulltime: focused team member efforts. Multitasking = 15% drop in productivity.


Experiment 8: Scheduling at 80% of team capacity produces better products.

  • Scheduling at 100% doesn’t give space to creativity.
  • No lost time: passionate workers keep thinking.
  • The 20% goes into new idea generation and process improvements.
  • Producing 20 great features is usually far more profitable than 100 competent features.


  • Allows employees to explore many options cheaply.
  • Gives time to prototype breakthrough solutions that sound crazy on paper.
  • Allows people to pursue passions.


  • Schedule 20% below possible velocity.
  • Hold periodic reviews of side projects and award interesting ideas.
  • Publicise and reward side projects that make their way into production.
  • Keep a public list of important things if anyone runs out of work (happens rarely).


Other productivity Techniques

Experimentation culture:

  • Fail faster to find success sooner.
  • Short iterations.
  • User metrics such as A/B testing.
  • State gate portfolio management.

Safety nets:

  • Test-Driven-Development.
  • Daily/weekly access to real customers.

Empower the team

  • Constraints-based requirements, not mandates from above.
  • Training.

See the original PDF.

Ruby on Rails vs ASP.NET MVC

I have been watching the ASP.NET MVC vs Ruby On Rails talk from the Norwegian Developers Conference 2010 and just want to make a number of quotes that I particularly liked. My apologies if I missed something, but I believe it would be interesting to read it for those who don’t want to spend 48 minutes to watch the whole thing. I believe the quotes here really summarise the atmosphere during the talk.
So here we go:
Rails was extracted from the real world work sweat, blood, tears, everything..
And it feels like it is.
ASP.NET MVC is a “me too” framework - trying to compete with other solutions. ASP.NET MVC is not part of the story for WebForms, not quite fit.
Aha. Try to tell WebForms guy about MVC :)
Microsoft promotes it as an option. Option for what?
These words from Rob Conery probably mean that MVC is something that was not “designed” to be mainstream. But rather an “option” for… well web development on .NET.
[About TekPub]…rewrite point for startup - reconsidered BizSpark. We wanted to stay small.We needed fast, easy framework. Up in the cloud, scalable, zero cost, everything.
There was a lot of buzz behind the decision to move to Rails by Rob Conery who was a .NET advocate. But it was a happy business AND technical decision.
Rails came out of a need. MVC hasn’t. NHibernate came out of a need. Entity Framework hasn’t. It’s always the same: solving problems they personally don’t have, so they are not in the context of their problems.
Microsoft projects often come out of suppositions.
That is very common with Microsoft. They have to be everywhere and at the same time nowhere. Anything “new” that comes out from Microsoft has already been adopted and they are just catching up.
Dell is rebuilding everything in ASP.NET MVC (instead of WebForms).
Good for them. In any case, ASP.NET MVC  IS a good thing to have in .NET world. And that’s my preference for Web development in .NET. Probably same goes with Dell.
MVC redirected the attention back to .NET after .NET Web developers started moving to Rails.
True. And in fact, most of the features of ASP.NET MVC were borrowed from Rails. So that was one of the easiest ways to hold .NET gurus back.
Why would .NET developers move to Rails?
Testing in rails is…uhh… it’s something you realise when you do it. Testing tools like RSpec and Cucumbet are just natural.
Deployment – Capistrano, Heroku – just unbelievable.
Cannot agree more on this. Testing “just works”, deployment “just works” it all “just works”.
In .NET we like to overcomplicate ourselves. Something too simplistic can’t be good. [No worries with SRP principle – objects aware of the persistence and similar].
In .NET we question more.
Speaking about myself, I do agree with it. Partially due to the nature of .NET (C#), partially because testing is harder, but also just because .NET developers try to solve problems in advance assuming those will exists (while in many cases they will not).
The MVC/MonoRail and Ruby On Rails have the same features.
Read on…
You have the same names of the features, but the experience is dramatically different. There’s incredible amount of software out of the box to do anything.
ASP.NET NVC hasn’t even got a plug-in mechanism.
So true! So true!
Why do we have to wait for 2 years to get the innovation that we already have available to us right now in Rails?
The rails community moves with small changes all the time, while .NET – with large in couple of years time. So for Rails it is a normal evolution, while for .NET it is rather a revolution.
[about Rails]… it means you are building a web site, and you are not rebuilding the stuff that has already been built. You are building Business.
Oh, yes. Having higher level plug-ins (nearly as Service-Oriented-Architecture components) it opens much more opportunities for business.
Not everybody is fortunate of making decisions at work [on choosing technology].
Try to push a new technology in a corporate environment with all the bureaucratic procedures. No f…ing way! Unless you are the one who decides.
Building web sites on .NET is about building web sites in a corporate environment. And that’s not about productivity. That’s not about really, really, really compressed budget where every decision is your life and death decision…[and then] It’s like we have a budget, but is is rather a suggestion.
Not much to comment on here. Don’t care about money – you are a .NET shop. Otherwise – probably Rails.
It’s about how fast you can turn your ideas into money.
Well said!
Most of the cost of long lived projects is re-learning. Ruby code is just scan and get it.
Indeed, Rails, would not even exist without such a beautiful language as Ruby. It makes code easy to understand.
In Ruby there is just less code to see where a problem might be. A lot less code! And the code is so usable that you can really spot the problems really quickly.
Again so true. Just have a look, for example, at some OSS projects. They do so much with so little code. That’s just amazing.

At the end, I must admit that I do like the .NET platform, but Ruby and Rails just feels much better to me and ALL of the things I currently do for myself, I do using Ruby/Rails.
It is just much more fun!


Twitter …

Remember running your site in .NET or RoR is not going to make a whole lot of difference in speed if your database architecture sucks or your website does alot of IO.

ex. alot of Google's products are written in Python but thats just a front alot of the actual business logic is probably implemented in the back in Java or whatever they prefer.
Check benchmarks on the web, java and .net have better performance.

Also, i'd like to know a big app (300 tables and millions of entries) on RoR.

Visual Studio is a big thing also.

But, normally i use rails for blogs and websites x)
I expected a serious comparison but instead I get an idiot ranting about no further innovation needed because he found something he likes.
So biased. One more thing, rails is designed as dsl for web apps while .net is platform for everything in windows.
haha, soooo biased…. and lame.
Alex Kaushovik
Rails is the best!
Long live to ROR!!!!
Good post. I'm a PHP/Rails developer considering venturing into .NET development, so I find this helpful.

Render Any ASP.NET MVC ActionResult to a String

I often see questions on the net about how to render a view to a string so it can be used somewhere.

My approach allows doing it without thinking about all the boilerplate code. Additionally not only the ViewResult can be rendered into a string but just about any type of the result. Here is example on how to return a JSON including the result of the view as additional information:


// Controller Action:
public JsonResult DoSomething() {
    var viewString = View("TheViewToRender").Capture(ControllerContext);
    return new JsonResult {
        JsonRequestBehavior = JsonRequestBehavior.AllowGet,
        Data = new {
            time = DateTime.Now,
            html = viewString


This can be done with 2 simple utility classes below. Just include them somewhere into your project.


    public class ResponseCapture : IDisposable {
        private readonly HttpResponseBase response;
        private readonly TextWriter originalWriter;
        private StringWriter localWriter;
        public ResponseCapture(HttpResponseBase response) {
            this.response = response;
            originalWriter = response.Output;
            localWriter = new StringWriter();
            response.Output = localWriter;
        public override string ToString() {
            return localWriter.ToString();
        public void Dispose() {
            if (localWriter != null) {
                localWriter = null;
                response.Output = originalWriter;
    public static class ActionResultExtensions {
        public static string Capture(this ActionResult result, ControllerContext controllerContext) {
            using (var it = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response)) {
                return it.ToString();

Enjoy and let me know if it works for you.


The Dode
Never mind. I found the problem. I miss the @model.
The Dode
Does this work with Razor?

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'TextBox' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.
After very long looking at the code, I noticed that I typed "@Model". it should be "@model".
I didn't know model is case-sensitive.
Dmytrii Nagirniak
Email me a sample project and I will have a look. Hard to say anything without seeing it.
It almost works..
If my view contains "@Model" on the first line. This line will be also in the output.

"@Model x.y" will be in the string as "x.y x.y"

any ideas ?
Thanks a lot, this helped!
Dmytrii Nagirniak
For the .NET 3.5 it will be needed to substitute the HttpContext on the controller and involves a bit more code, but still is possible. Have a look for an example
I have a question. How can I do it in .NET Framework 3.5? in thi, Output property doesn't have a setter method.
Could you help me please?
Dmytrii Nagirniak
You can write:

View("ViewName").With("name", "Dima").With("likes", "Ruby").Capture(ControllerContext)

with this simple extension method:

public static ViewResult With(this ViewResult vr, string key, object value) {
vr[key] = value;
return vr;
This is a cleaner solution that others out there. Does anyone have an example if the Action also uses parameters?

RSpec 2.0 Cheat Sheet by Example

The Ruby developers know and hear a lot of buzz about RSpec BDD framework. The RSpec specifications code is so easy to read, but for my it is often hard to write. So I intend to produce a small cheat-sheet for RSpec syntax for myself.

Most of it come from the RSpec Book in rather unusual way. As a RSpec itself (unfortunately not runnable for now).

Please feel free to use it in any way you want and correct the mistakes.

So here we go: