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

Tuesday, April 17, 2012

How to list files in MB on Linux

ls -l --block-size=1M

How to limit the number of records returned by mysqldump

How to restrict the size of mysqldump?

mysqldump --where="true LIMIT 1000" -u user-name -p db-name > exported-data.sql

How to list all the loaded gems on a server

Open a Rails console and type:

 $:.each do |x|
   p x
 end;
 p "hi"

$: variable contains the list of all gems loaded into the system. I am suppressing the output of array of those gems by doing ; p "hi" after the end.

sh: irb: not found

On Ubuntu you need to install irb :

sudo apt-get install irb

no such file to > load -- net/https when starting Rails server

Install libopenssl-ruby1.8.

Monday, April 16, 2012

How to convert ppk file to normal ssh key in Ubuntu 10.04

sudo apt-get install putty-tools
puttygen your_file.ppk -O private-openssh -o new_file_name

scp permission denied publickey

How to copy a file from a remote Ubuntu server to your machine using scp and private key


scp -i /path/to/your_private_key_file user@your_host_or_ip:/path/on/the/remote/machine/file_name .

This will copy file_name to current directory in your local machine

no such file to load -- net/https on Ubuntu 10.04

sudo apt-get install libopenssl-ruby

How to install mysql gem on Ubuntu 10.04

sudo apt-get install mysql-server mysql-client
sudo apt-get install libmysql-ruby libmysqlclient-dev
sudo gem install mysql
Install mysql database connector by running the above commands.

How to install Nokogiri on Ubuntu

libxml2 is missing error message is due to missing libraries. Install the pre-requisite library with:


sudo apt-get install libxslt-dev libxml2-dev

How to install curb gem on Ubuntu 10.04

sudo apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev

How to install bson_ext on Ubuntu 10.04

Using Bundler 1.1.3 for some reason does not work. Solution:

sudo gem install bson_ext -v=1.0.1

installs the gem.

How to ssh using a given private key

ssh -i /path/to/your_private_key_file user-name@your-ip-address-here

Friday, April 13, 2012

It is recommended that your private key files are NOT accessible by others

I was getting this error : Permissions 0644 for '/home/bparanj/Downloads/my-file.ssh' are too open.
when I was ssh ing into a box.
 
Solution:
sudo chmod 600 your-private-key.ssh

Monday, April 09, 2012

Rails Recipes 3rd Edition

The recipe: Focus Your Tests with Mocking and Stubbing show how to use stubs and mocks. It violates one of the testing best practices. DO NOT mock external services. Why? Because you cannot drive the design of a third party library. For a good discussion on this topic read the "Growing Object Oriented Software Guided by Tests".

I scanned through the book, I was a bit disappointed. Lot of the recipes are just from the old book.