• Home
  • About Me
  • Musings
  • Programming
  • Open Source
  • Computers
  • Society
  • Books
  • Design
  • Movies
  • Rake Migrate – Aborted!

    Trying out a sample application using rails, I faced a problem, getting the database tables created (migrated) using ‘rake’. Running the command

    1
    
    rake migrate

    threw the following error

    1
    2
    3
    4
    5
    
    (in /home/rajesh/Programming/rails/todolist)
    rake aborted!
    Don't know how to build task 'migrate'
     
    (See full trace by running task with --trace)

    The search on internet forums only said that ‘rake migrate’ should do the job, but to no avail.

    The actual command to get the tables created is

    1
    
    rake db:migrate

    Hope this helps!

    Free!

    Free, I have broken free, free from proprietary software in my personal life.

    I have decided to use only software that are free. Making that move wasn’t that easy, got to leave the comfort of Windows and MS Office, the one’s I have been used to since I started using computers. I have not broken free from the entire MS world of software, coz I make my living coding in C#, I have a Express Edition of Visual Studio for the learning that is needed to keep me hands-on up-to-date on the technology.

    For office suite, I have resorted to OpenOffice 3. It does the job well for me. Chrome for a browser and NetBeans IDE for development is all I need. It is a nice feel to be just using free software, at least I know I have not stolen other’s money. I respect another software engineer, for his work.

    Here is a nice quote, from the famous film, The Shawshank Redemption where Morgan Freeman says,

    “Some birds aren’t meant to be caged, their feathers are just too bright, and when they fly away… the part of you that knows it was a sin to lock them up does rejoice…” – The Shawshank Redemption.

    I feel I’m one of those birds which is not supposed to be caged, and I have grown one feather that is going to help me in my flight..

    Cows and Bulls game

    Cows and Bulls is a small word building game, implemented in C# as a Windows Application. Feel free to download and play around with it. CowsNBulls is in its version 0.0.0.1 and in beta testing.

    The game began in the last bench of my college, MIT where we used to play this game. It was Barath Kumar, my friend who introduced this game to us. Thought of computerizing it and so here it is. 

    Download the game and do let me know your comments.

    Edit: For a web only version of the game, visit: www.raasukutty.net

    The web only version is developed using HTML, Javascript and jQuery. For all the developers out there, check out the source using view source.  All suggestions are welcome.

    Happy gaming!

    Rails: Error with MySQL in welcome screen

    After installing rails, to test it, I created a rails application using the following command at the command prompt.

    1
    
    rails -d mysql todolist

    Pointing to http://localhost:3000 displayed the expected Welcome message.

     

    Welcome Message

    Welcome Message

    But then clicking on the About your application’s environment displayed a error message like below.

     

    Error in Rails

    Error in Rails

    That’s the least thing I expected. Digging into the development.log file located in the project log directory, displayed the actual error message, “Client does not support authentication protocol requested by server; consider upgrading MySQL client”.

    Looking into the web for help, the problem with the way rails (as a client) was trying to access MySQL server. Rails uses a old password hashing used in MySQL 4.1, and the current version that I am running is MySQL 5.0. This had caused the error. The fix is running the following script in mysql prompt.

    1
    
    mysql> SET PASSWORD FOR 'railsuser'@'host' = OLD_PASSWORD('password');

    That fixes it and you would get the following screen.

    Success message

    Success message

    C#: Sorting a DataTable

    Sorting the result in a DataTable’s select command can be done as below.

    1
    2
    
    DataTable table = dataSet.Tables[0]; // Get a datatable from the dataset.
    DataRow[] row = table.Select("id = 500", "name desc, age asc"); // Get from table, where id = 500 order by name desc and age desc.

    The first argument to the select is the filter condition, the second argument is the sort option.

    Trying to use,

    1
    2
    
    DataTable table = dataSet.Tables[0]; // Get a datatable from the dataset.
    DataRow[] row = table.Select("id = 500 order by name desc, age asc"); // Get from table, where id = 500 order by name desc and age desc.

    will result in a error “Missing operand before order”.

    C#: Null coalescing operator

    C# has a ?? operator, which is called the ‘Null coalescing operator’. The ?? operator is a infix operator used on nullable types or objects. If the operand on the left is null, it returns the value of the expression on the right, else it is the left operand itself.

    Here is an example showing how it is used.

    1
    2
    3
    4
    5
    6
    7
    
        int result;
        int? num = null; // num is null
        result = num ?? 10; // sets result to 10.
        Console.WriteLine(result);
        num = 5; // num is 5.
        result = num ?? 10; // sets result as 5.
        Console.WriteLine(result);

    Installing Rails : Local gem install

    After a unsuccessful installation of rails that took nearly 5 hours, I decided to get rid of Ruby. But the geek inside me, wanted me to go for the kill. That started the quest of successfully installing and deploying rails on WAMP server. Here are the steps.

    Requirements:

    Installation Instructions:

      Install WAMP and Ruby on the machine.
      Extract the Ruby Gems Zip file to any location and open a command prompt window.
      Run setup.rb from the command prompt in the location where the zip was extracted.
      Open command prompt and navigate to the folder where the downloaded gems are located.
      Install Rake using the following command.
    1
    
    gem install rake-x.x.x --local
      Here x.x.x stands for the version number, for example, to install rake-2.3.2.gem run it as ‘gem install rake-2.3.2 –local’.
      Install Active Support, Active Record, Action Pack, Action Mailer and Rails using the command as below
    1
    
    gem install gem-x.x.x --local
      Replacing the gem- to the current gem to be installed.

    This will get Rails installed on the machine. The total install time should be close to 10 minutes.

    Math.Round

    Math.Round has been improved in C#.
    Consider the below piece of code:

    1
    2
    3
    
    Console.WriteLine(Math.Round(10.4)); // Rounds to 10.0
    Console.WriteLine(Math.Round(10.7)); // Rounds to 11.0
    Console.WriteLine(Math.Round(10.5)); // Rounds to 10.0

    There is nothing surprising about the first two statements.
    In the third statement however, 10.5 is rounded to 10 not 11. C# provides for a way to specify how the middle point has to be treated.
    A enumeration ‘MidpointRounding’ that defines how mid points are treated.

    1
    2
    3
    4
    5
    
    Console.WriteLine(Math.Round(10.5, MidpointRounding.AwayFromZero)); // Rounds to 11.
    Console.WriteLine(Math.Round(10.5, MidpointRounding.ToEven)); // Rounds to 10.
    Console.WriteLine(Math.Round(11.5)); //Rounds to 11.
    Console.WriteLine(Math.Round(11.5, MidpointRounding.AwayFromZero)); // Rounds to 12.
    Console.WriteLine(Math.Round(11.5, MidpointRounding.ToEven)); // Rounds to 12.

    ‘AwayFromZero’ – Rounds the number to the next highest value.
    ‘ToEven’ – Rounds the number to Even number.

    So for a odd fraction, the default round will round it to the lesser number and any of the above overloads will take it to the Next number.

    Implicit Variable in C# 2008

    C# 2008 allows for creating implicit variables using the ‘var’ keyword. But the usage of ‘var’ cannot be truly justified for just declaring a ‘int’ or ‘string’ in code as shown in example below.

    1
    2
    3
    4
    
    var intNum = 5;
     
    Console.WriteLine("intNum is a: {0}", intNum.GetType().Name);
    Console.WriteLine("intNum is defined in: {0}", intNum.GetType().Namespace);

    The output of the above program is

    1
    2
    
    intNum is a: Int32
    intNum is defined in: System

    Here instead of using int in the declaration, we have used ‘var’ keyword. There is no difference between using ‘int’ or ‘var’ in the above example.
    The real usage of ‘var’ comes in LINQ. Consider the following code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    int[] numbers = { 10, 20, 30, 40, 8, 7, 6, 2 };
    var resultSet = from i in numbers where i < 10 select i;
     
    foreach (var i in resultSet)
    {
    Console.Write("{0}", i);
    }
     
    Console.WriteLine("resultSet is a: {0}", resultSet.GetType().Name);
    Console.WriteLine("resultSet is defined in: {0}", resultSet.GetType().Namespace);

    Here resultSet is declared as a ‘var’. From the code, we understand that anytime, the resultSet will be an array of integers. But ‘resultSet.GetType().Name’ gives a surprising result.

    1
    2
    
    resultSet is a: d__0`1
    resultSet is defined in: System.Linq

    So ‘var’ has its best usage in LINQ. So why use it in a normal program when the datatype can be used in itself.

    Avoid flickering in dynamically rendered control in Windows App

    Here is a tip shared by Kannan, a colleague of mine on how to avoid flickering when rendering controls dynamically in a Windows Application.

    Enable double buffering, so that the flickering does not happen.

    Add the following after the ‘InitializeComponent’ method.

    1
    2
    3
    
          SetStyle(ControlStyles.UserPaint, true);
          SetStyle(ControlStyles.AllPaintingInWmPaint, true);
          SetStyle(ControlStyles.DoubleBuffer, true);

    That’s it.