Friday, March 18, 2011

Ruby Cheat Sheet for .NET Developers

....or anybody who sucks at OSX/Linux.

I've been learning Ruby on Rails over the last couple of months and when you've been programming on Windows with Visual Studio for as many years as I have it's a major learning curve to switch to programming Ruby on OSX or Linux. So I've created a cheat sheet to help me with all the little details I routinely have to look up.

Ruby on Rails Command Line

Preview Site:
rails server
Preview Site as Production:
rails s -e production
Test DB and be able to rollback changes:
rails console --sandbox
Reset DB:
rake db:reset
Modify DB for real:
rails console
View ActiveRecord Raw SQL:
tail -f log/development.log
Migrate Development DB
rake db:migrate (DEV)
Migrate Test DB
rake db:test:prepare
Migrate Production DB
rake db:migrate RAILS_ENV=production
Create a New DB Migration:
rails generate migration [MIGRATION NAME] 
rails generate migration add_email_uniqueness_index
View Routes:
rake routes
Start Autotest:
autotest


Output debug info in Model:
logger.debug @user.attributes.inspect
Add Debug info to Layout:
<%= debug(params) if Rails.env.development? %>
Routes:
NAMED ROUTE            PATH
users_path             /users
user_path(@user)       /users/1
new_user_path          /users/new
edit_user_path(@user)  /users/1/edit
users_url              http://localhost:3000/users
user_url(@user)        http://localhost:3000/users/1
new_user_url           http://localhost:3000/users/new
edit_user_url(@user)   http://localhost:3000/users/1/edit

RESTFUL Routes:
GET      /photos           index     display a list of all photos
GET      /photos/new       new       return an HTML form for creating a new photo
POST     /photos           create    create a new photo
GET      /photos/:id       show      display a specific photo
GET      /photos/:id/edit  edit      return an HTML form for editing a photo
PUT      /photos/:id       update    update a specific photo
DELETE   /photos/:id       destroy   delete a specific photo


Assign if variable is undefined:
||=  
@current_user ||= user_from_remember_token
Variable Scope:
$            A global variable
@            An instance variable
[a-z] or _   A local variable
[A-Z]        A constant
@@           A class variable
http://www.techotopia.com/index.php/Ruby_Variable_Scope 


Upgrade to the latest version of rvm
rvm update --head
Install a version of Ruby
rvm install 1.9.2
Working with gemsets
rvm info                        # show the current environment
rvm 1.8.7                       # use the ruby to manage gemsets for
rvm gemset create project_name  # create a gemset
rvm gemset use project_name     # use a gemset in this ruby
rvm gemset list                 # list gemsets in this ruby
rvm gemset delete project_name  # delete a gemset
rvm 1.9.1@other_project_name    # use another ruby and gemset
Default for Project:
echo "rvm 1.9.1@MyProject" > ~/projects/MyProject/.rvmrc

Add gem to Gemfile
Run:
bundle install
or 
bundle update
or (for self contained)
bundle pack


Extract tar.gz:
tar -zxvf yourfile.tar.gz
Find a file:
find . -name "controller.rb" 
http://helpdesk.ua.edu/unix/tipsheet/tipv1n10.html
Delete Folder:
rm -rf
Add JPG File Extension to Multiple files:
for f in *; do mv "$f" "$f.jpg"; done

Remove file from repo:
git rm --cached


GitHub

Checkout:
git clone git://github.com/crdeutsch/MVC3-Boilerplate.git
Publish:
git push origin master


Enable Git Flow
git flow init
Start a Feature
git flow feature start myfeature
Finish a Feature
git flow feature finish myfeature


Create App:
heroku create
Publish:
git push heroku master
Migrate DB:
heroku rake db:migrate
View Logs:
heroku console
File.open('log/production.log', 'r').each_line { |line| puts line }


Compile CSS:
compass compile
Watch project for changes and compile whenever it does:
compass watch


New site:
staticmatic setup my_site
Preview:
staticmatic preview my_site
Build:
staticmatic build my_site

OSX

Screen Capture:

Full Screen:
Hold down Apple key ⌘ + Shift + 3 and release all
Portion of your screen:
Hold down Apple key ⌘ + Shift + 4 and release all key
Application window:
Hold down Apple key ⌘ + Shift + 4 and release all key
Now, You will see the mouse cursor will change to +
Press the space bar once


This list is far from comprehensive, but it's pretty much everything I've had to google at least once to figure out and I plan to keep adding to it as I progress.

If you have some nuggets to share add them to the comments.

If lots of people want to contribute we should probably move this to a different format (Wiki, Markdown?) but until then I'll keep maintaining it here.