Software Development

Thursday, May 31, 2012

-bash: mongod: command not found


export PATH=$PATH:/usr/local/mongodb/bin
For Mongodb 2.0.3
mongod --dbpath /Users/bparanj/data/mongodb
The dbpath is the path to the mongodb.config file
which contains :
dbpath=/Users/bparanj/data/mongodb

Deploying to a VPS

To get the http://railscasts.com/episodes/335-deploying-to-a-vps?view=asciicast working I had to do :

1. apt-get install sqlite3 libsqlite3-dev
2. Add : gem 'pg' and gem 'unicorn' to Gemfile

Wednesday, May 30, 2012

undefined local variable or method `autotest' for main:Object

This error happens when you have this : require autotest/fsevent
in your ~/.autotest file, change it to : require 'autotest/fsevent'

Thursday, May 10, 2012

Data Driven Unit Test


How do you avoid loops in your specs?

Like Alex points out, this is a test smell according to Gerard Mezos. In .NET they have something called as data driven unit test. In Ruby we can accomplish the same thing using the following:

  def data_driven_spec(container)
    container.each do |element|
      yield(element)
    end
  end

and in your test you can do :


  it "should return back slash pre-pended to all special characters" do
 
    SPECIAL_CHARACTERS = ["?", "^", "$", "/", "\\", "[", "]", "{", "}", "(", ")", "+", "*", "." ]
 
    data_driven_spec(SPECIAL_CHARACTERS) do |special_character|
      result = Regexpgen::Component.match_this_character(special_character)
      result.should == "\\#{special_character}"    
    end
  end

The code is taken from my regexpgen ruby gem. My test differs from the message that goes in to the it method. I have raised the level of abstraction, whereas Alex substitues the a variable to vary the message.

Wednesday, May 09, 2012

`bin_path': can't find gem rspec-core

use rspec instead spec for running the spec file.

Creating a Ruby gem with RSpec as the test framework

1. bundle gem 'your-gem-name'
2. Run : rspec --init from your gem directory to generate the spec/spec_helper.rb and .rspec files.

.rspec has the configuration such as color and format of the output when specs are run.


Tuesday, May 01, 2012

irb tricks

1. touch ~/.irbrc
2. Edit the .irbrc and add the following code:


require 'pp'
require 'rubygems'

class Object
  def ls
    %x{ls}.split("\n")
  end
 
  def pwd
    Dir.pwd
  end
 
  def cd(dir)
    Dir.chdir(dir)
    pwd
  end
end

alias p pp

3. Now whenever you open an irb session you can now list the directory contents by doing ls, print working directory by pwd and change directory by doing cd "directory-name".

Friday, April 27, 2012

Solution for Ruby Koans

You can download by solution from here https://github.com/bparanj/ruby-koans-edgecase.

This took me 5.5 hours to complete. I broke this down into 11, 30 minutes sessions over a period of two weeks.

Things that I learned:

1. It was a "aha" moment when I worked through a series of tests that described the behavior of a class. You have to do the exercises to get the feel for describing the behavior or specification of a class.
2. It helps you to find your strengths and weakness so you will know where to focus your learning efforts.

How to install rubygems from source on Ubuntu 10.04

1. wget http://rubyforge.org/frs/download.php/69365/rubygems-1.3.6.tgz
2. Extract : tar xvzf rubygems-1.3.6.tgz
3. cd to rubygems directory and run : ruby setup.rb
4. sudo ln -s /usr/bin/gem1.8 /usr/bin/gem


Check version by either : gem -v or gem env

`merge': can't convert String into Hash

This error happens when you have syntax error in your .gemrc file. Fix it. gem env will work again.

How to install Ruby 1.8.7 on Ubuntu 10.04

sudo aptitude install ruby1.8-dev ruby1.8 irb1.8 libreadline-ruby1.8 libruby1.8 libopenssl

sudo ln -s /usr/bin/irb1.8 /usr/bin/irb
sudo ln -s /usr/bin/ruby1.8 /usr/bin/ruby
 
Check the Ruby version : 
 
ruby -v 


How to count the number of files in a given directory in Unix

ls -f | wc -l

undefined method `name' for RubyToken::TkLPAREN

I got that error message when I installed httpclient on Ubuntu 10.04. The gem got installed the error is due to the rdoc. You can turn off the installation of rdoc by appending no rdoc switch to the .gemrc file :

echo 'gem: --no-ri --no-rdoc' >> ~/.gemrc

The above command will append the 'gem: --no-ri --no-rdoc' to your .gemrc file.


How to remove Open JDK on Ubuntu 10.04

sudo apt-get purge openjdk-\* icedtea-\* icedtea6-\*

How to append to a file in Ruby


    my_file = File.open('file_name', 'a')

Once you open the file in append mode, you can write to it. It will get appended to existing contents of the file.

How to list all files with a given extension in Ruby

Dir["*.xml"].each do {|f| p f}

Dir will return all xml files as an array. The block prints the name of the xml file.

How to list all files in a directory in Ruby

Dir.foreach(".") do {|f| p f}

This will print all the file names in the current directory including files that begin with a period.

How to copy local file to a remote Ubuntu server

 scp -i ./your-key.ssh foo.rb user-name@101.101.101.101:.

Where your-key.ssh and foo.rb are in the directory where you are running scp. Since you have . after : at the end of ip address, the local file foo.rb will get copied to the root directory of your remote machine.

How to comment multiple lines in Ruby

Use:
=begin
your code goes here
on multiple lines
=end

multiline comments

How to compare Time in RSpec


When you are dealing with Time the == operator will not pass the test when you compare the time objects. Quick fix is to call to_s and use == to compare the time string values. Like this:

my_timestamp.to_s.should eq("Mon Apr 09 20:56:25 UTC 2012")

Reference:

Comparing time in rspec

Monday, April 23, 2012

no such file to load -- zlib

1. Go to the directory where you extracted the Ruby package:
    Ex : cd ~/temp/ruby-1.8.7-p248/ext/zlib
2. ruby extconf.rb
3. sudo make && sudo make install