Friday, March 28, 2014

Checking if a number is prime in Ruby

Go to irb :

 > require 'prime'
 => true
 > Prime.prime?(2)
 => true
 > Prime.prime?(3)
 => true
 > Prime.prime?(10)
 => false

Wednesday, March 26, 2014

Configuring Guard to Display output in Red Green Colors

Change the Guard file guard method to:

guard :rspec, cmd: 'rspec --color --format doc' do
... contents unchanged

end

From the root of the project run: guard
You will now see the colors in the terminal whenever the test is run.

Friday, March 21, 2014

Alternative to using take and drop methods in Array class

The take method takes elements from the beginning whereas the drop takes the elements from the end of the array. These methods require some mental mapping of the method names to what it does. I was thinking about reducing this gap. Here is the experiment in the IRB:

2.0.0 > a = [1,2,3,4]
 => [1, 2, 3, 4]
2.0.0 > a.first(2)
 => [1, 2]
2.0.0 > a
 => [1, 2, 3, 4]
2.0.0 > a.last(2)
 => [3, 4]
2.0.0 > a
 => [1, 2, 3, 4]

It just works! This is one of the reason why I love Ruby.

Monday, March 17, 2014

How to count number of files in a bucket in S3 recursively

1. git clone https://github.com/s3tools/s3cmd
2. cd s3cmd
3. s3cmd --configure
Enter the Amazon credentials.
4. ./s3cmd ls --recursive s3://your-bucket/another-bucket/foo

This will recursively find all the folders under foo and list all the files.

5. You can pipe it to wc to get the number of files like this :
     ./s3cmd ls --recursive s3://your-bucket/another-bucket/foo | wc -l

   

Loading rubyzip gem in irb

LoadError: cannot load such file -- rubyzip

Instead of : require 'rubyzip' use require 'zip'

Turn off Bonjour Notifications in Cyberduck


On Mac OS, run the following command in a terminal:

defaults write ch.sudo.cyberduck rendezvous.enable false

Reference:

Diable Bonjour in Cyberduck

Using MIME types outside of Rails 4

In a IRB session:

require 'action_dispatch/http/mime_type.rb'
 => true
> MIME::Types.type_for('a.pdf')