Datos personales

miércoles, 22 de enero de 2014

Ruby on Rails automation script for new project

When i started to develop rails applications(short time ago) setup the project was the first: name, database, rvmrc, ruby version, gems and maybe some third-party angular, bootstrap, etc. I get bored quickly, because i had to do it each time for a project in order to practice rails, so i wrote a simple script to do it fast and start to code happy.


To do:

#Install ruby version
$rvm install 2.0.0

Work directory sample bootstrap - postgreSQL
- railsprojects
|- railsnew.sh
|- Gemfile
|- dist/
Setup Gemfile pg & haml

source 'https://rubygems.org'

gem 'rails', '4.0.0'
gem 'pg'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
gem 'haml'

group :test,:development do
  gem 'rspec-rails'
  gem 'pry'
end

group :test do
  gem 'capybara', "2.0.2"
  gem 'fabrication'
  gem "shoulda-matchers"
end

Download bootstrap
$wget https://github.com/twbs/bootstrap/releases/download/v3.0.3/bootstrap-3.0.3-dist.zip
$unzip bootstrap-3.0.3-dist.zip 
Script railsnew.sh
echo "Running"
rails new "$1" --database=postgresql

cp Gemfile $PWD"/$1"
cd $1
echo "$1" > .ruby-gemset
cat .ruby-gemset
echo "$2" > .ruby-version
cat .ruby-version

cp ../dist/js/bootstrap.js  $PWD/app/assets/javascripts/
cp ../dist/css/bootstrap.css  $PWD/app/assets/stylesheets/
Permissions
sudo chmod +x railsnew.sh
To run the script we need to pass 2 parameters "appname" "ruby-version"
#Our ruby list
$rvm list

rvm rubies

=* ruby-1.9.3-p429 [ i686 ]
   ruby-2.0.0-p195-perf [ i686 ]

# => - current
# =* - current && default
#  * - default

Execution & verification
$./railsnew.sh testapp ruby-1.9.3-p429
$cd testapp
$rvm current
$bundle
That's it, you can add/edit what you need for your projects

domingo, 5 de enero de 2014

Benchmark Ruby Special Pythagorean Triplet

More info about Problem 9

More info about Benchmark

System Values
Ubuntu: Release 12.04 (precise) 32-bit
Kernel Linux 3.5.0-45-generic
Processor : Intel® Pentium(R) Dual CPU T2330 @ 1.60GHz × 2
Memory: 2.0 GiB

#!/usr/bin/ruby
require 'benchmark'
class PythagoreanTriplet

  def square val
    val * val
  end
  
  def add_square(a,b)
    square(a) + square(b)
  end

  def triplet(a,b,c)
    square(c) == add_square(a,b) ? true : false
  end

  def triplet_addition(a,b,c)
    a+b+c 
  end

  def get_product_abc
    for a in 0..500
      for b in 0..500
        for c in 0..500
          if a < b && b < c
            triplet(a,b,c) ? find = triplet_addition(a,b,c) : find = nil
            printf '%d - %d - %d ', a,b,c if find == 1000
            break if find == 1000
          end
        end
        break if find == 1000
      end
      break if find == 1000
    end
  end

end

pt = PythagoreanTriplet.new
puts Benchmark.measure { pt.get_product_abc }

Results

No break statement

200 - 375 - 425  
42.950000   0.170000  43.120000 ( 43.553589) <- Time

Break statement

200 - 375 - 425  
25.200000   0.040000  25.240000 ( 25.396476) <- Time