Debugging Using Pry

Add the following gems to the Gemfile and bundle install

File: Gemfile

1
2
3
gem 'pry'
gem 'pry-nav'
gem 'pry-remote'

The pry-nav gem allows us to use the following command for adding breakpoints into our application

1
binding.pry

Add the following shortcuts to ~/.pryrc to enable navigation while running debugging

File: ~/.pryrc

1
2
3
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'

The pry-remote let us use pry remotely

Any questions on this, please feel free to ask. We’re here to help…

Comments

Change Computer/Host Name Mac OsX

One of the most common problems that we come across is when the programmer tries to setup the Mac OS X - name/hostname for the computer, since it takes up most space in the Terminal bash prompt and since the default ones are not so fancy, when accessed over the network.

  1. Click the “Apple” menu in the top-left of the screen. Click on “System Preferences.”

  2. Click the “Sharing” icon. At the top of the resulting screen, you will see your current computer name. To change it, click into the field, delete the existing name and enter your desired computer name.

  3. Click on the “Edit” button underneath the computer name field to change the local hostname. The hostname can be used to access your computer over the network as an alternative to using a numeric IP address and is particularly relevant if you are using a network with a mix of Apple Macs and Microsoft Windows PCs.

  4. Close the window to save your changes.

Source Link

Any questions on this, please feel free to ask. We’re here to help…

Comments

Setting Up Heroku

Here are the steps to push a rails project to Heroku

  1. Install Heroku
1
gem install heroku

2. Create a heroku login with email and password

1
heroku Create

3) Change the name of the app

1
heroku apps:rename demo-app

devcenter rename link

4) Push the file to heroku

1
git push heroku master

devcenter git link

5) Check the database

1
heroku run rake db:version

devcenter rake link

7) Perform a db migrate

1
heroku run rake --trace db:migrate

8) Console:

1
heroku run console

Further reading:

devcenter link

Any questions on this, please feel free to ask. We’re here to help…

Comments

RVM Setup and Environments

RVM is the Ruby environment manager. Here are some of the commonly used commands.

Installing rvm in unix/mac

1
curl -L https://get.rvm.io | bash -s stable

Setting up the bash profile for enabling rvm

File: .bash_profile

1
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # This loads RVM

Dont forget to source it, to load the ~/.bash_profile if you’re using the same console

1
source .bash_profile

Installing new Ruby version

1
rvm install 1.9.3-p327

Listing installed ruby versions

1
rvm list

Updating rvm version from head

1
rvm get head

Setting the default ruby version

1
2
rvm alias create default 1.9.3
rvm use 1.8.7-p302 --default

For more help use:

1
rvm --help

Any questions on this, please feel free to ask. We’re here to help…

Comments

Using Deligate in ActiveRecord to Avoid Dots

Adding deligate method in the model

There are various situations where we might be using methods from the child model from its parents model.

Common example is user and posts

1
2
3
4
5
6
7
class Post
  belongs_to :user
end

class User
  has_many :posts
end

To make a call to find a post with category ‘ruby’ for a User

1
User.first.posts.find_by_category('ruby')

If this is being used in multiple places, adding a deligate will make our life easy, thereby avoiding mulitple dots. Also User model doesnt have to know the inner methods of the posts model and a logic change in the model Post would not require multiple changes

1
2
3
4
class Post
  belongs_to :user
  delegate :find_by_category, :to => :user, :allow_nil => true
end

now the following code will work.

1
User.first.find_by_category('ruby')

There is an option to allow prefix as well

Any questions on this, please feel free to ask. We’re here to help…

Comments

Setting Up RSpec and Cucumber

Including RSpec in your code:

Add the following in your Gemfile File: Gemfile

1
2
3
group :test, :development do
  gem "rspec-rails", "~> 2.0"
end
1
bundle

Installing RSpec

1
rails g rspec:install

Configuring RSpec

File: config/application.rb

1
2
3
4
config.generators do |g|
  g.test_framework :rspec
  g.integration_tool :rspec
end

Now adding a new scaffold or resource will automatically include the RSpecs to help test the code

1
2
3
rails g scaffold book title author
rake db:migrate
RAILS_ENV=test rake db:migrate

Running the Specs

1
rspec spec

You can also specify the file name and the line number for running individual specs

Including Cucumber in your Code

Add the following in your Gemfile File: Gemfile

1
2
3
group :test, :development do
  gem 'cucumber-rails', require: false
end

Installing Cucumber

1
rails g cucumber:install

For learners who are looking to setup the cucumber for the first time

Use Traing Wheels

File: Gemfile

1
gem "cucumber-rails-training-wheels", :group => :test

Installing Cucumber rails generate cucumber_rails_training_wheels:install

Set up Cucumber Training Wheels before Scaffolding

1
2
rails generate cucumber_rails_training_wheels:feature post title:string body:text number:integer published:boolean
rails generate scaffold post title:string body:text number:integer published:boolean

Testing the Features

1
cucumber features

If you face Issues with Tableish in your features

Replace:

1
tableish('table tr', 'td,th')

With:

1
find('table').all('tr').map { |row| row.all('th, td').map { |cell| cell.text.strip } }

Any questions on this, please feel free to ask. We’re here to help…

Comments

Configuring Mac Vim for Programming

  1. Download and install the latest mvim from

Macvim Link

  1. Clone the latest dotvim configuration from the Web.

Here’s one good source:

Dotvim Link

  1. Follow the steps given in the dotvim config readme to Setup .vim and .vimrc files for enabling the best settings

  2. Make sure that the git source for dotvim is updated regularly.

Any questions on this, please feel free to ask. We’re here to help…

Comments

Git Alias

Following are some of the common git aliases, people use. Add them to ~/.bash_profile

1
2
3
4
5
6
7
8
9
10
alias g='git'
alias gst='git status'
alias gs='git push'
alias gc='git commit'
alias gca='git commit -a'
alias ga='git add'
alias gco='git checkout'
alias gb='git branch'
alias gm='git merge'
alias gd="git diff"

An additional syntax highlighting can be provided as well with the following:

1
2
export CLICOLOR=1
export LSCOLORS=gxfxaxdxcxegedabagacad

Any questions on this, please feel free to ask. We’re here to help…

Comments

Mountain Lion and Homebrew

SOURCE

Get Mountain Lion and Homebrew to Be Happy:

1) Install XCode 4.4 into /Applications

Get it from the App Store.

2) Install Command Line Tools

In XCode’s Preferences > Downloads you can install command line tools.

3) Let Everyone Know Where XCode Is

1
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

4) Install X11

Visit http://xquartz.macosforge.org/trac/wiki and download and install 2.7.2+.

You will need to fix the symlink it makes:

1
ln -s /opt/X11 /usr/X11

5) Reinstall your brews

1
brew list

Will tell you what you need to check. Try out everything one by one and when one doesn’t work brew remove it and then reinstall it. If the install doesn’t work, try brew install it –use-gcc to prevent llvm from getting in the way. You might need to install libxml2 and link it manually to make sure you don’t get the wrong xml2-config if your path:

1
2
3
brew install libxml2
brew link libxml2
Run brew doctor and fix anything it tells you.

You may also need to symlink gcc for certain ruby gems and other stuff like that:

1
sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2

Any questions on this, please feel free to ask. We’re here to help…

Comments

GCC Command Line Tools

When the bundling gems fail in Mac OS X, Download the latest GCC standalone file from the link below:

gcc standalone tools

Any questions on this, please feel free to ask. We’re here to help…

Comments