Skip to content

Instantly share code, notes, and snippets.

@Jacke
Forked from wolframarnold/2011-07-18.ruby_class.md
Created August 17, 2013 13:24
Show Gist options
  • Save Jacke/6256829 to your computer and use it in GitHub Desktop.
Save Jacke/6256829 to your computer and use it in GitHub Desktop.

Hello Class!

This is a Gist, a pastable area on the web that I'll be using to save code snippets off, so you can copy and paste from here, and don't have to worry about missing some things I type.

Resources

Slide presentation (while in class): http://192.168.1.84:9090/

Ruby Documentation: http://ruby-doc.org/core/

Download exercises from:

git clone https://github.com/wolframarnold/learn_ruby.git
cd learn_ruby

Rails API:

http://railsapi.com/

Git installation

Follow instructions here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:install_git

RVM

If your system has RVM installed (not critical to have this for our needs), then switch to the RVM ruby before you run any exercises:

rvm use 1.9.2

Class Methods, Class Data, Instance Methods, Instance Data Example

class Adder

  @total_additions = 0

  def Adder.incr_additions
    @total_additions += 1
  end

  def Adder.total_additions
    @total_additions
  end

  def self.foo
    "I'm a class method"
  end

  def initialize
    @sum = 0
  end

  def add_to(num)  # instance method
    Adder.incr_additions
    @sum += num
  end

#  attr_reader :sum
  #def sum
  #  @sum
  #end

#  attr_writer :sum
  #def sum=(num)
  #  @sum = num
  #end

  attr_accessor :sum # same as attr_reader and writer
end

Rails Tutorial

https://www.e-junkie.com/d/?t=89E04444TP8615939&c=2pl8

RVM Setup

If you have RVM (not critical for this class), run this:

rvm use 1.9.2
rvm gemset create rails3
rvm gemset list_all
rvm use 1.9.2@rails3

Create the first app

rvm use 1.9.2@rails3  # if you use RVM
gem install rails
rails new first_app

Heroku Deployment

  • Set up ssh key
  • heroku keys add
  • heroku create <appname>
  • git push heroku master

Adding RSpec to the Gemfile

group :development do
  gem 'rspec-rails', '~> 2.6.0'
end
group :test do
  gem 'rspec', '~> 2.6.0'
end

Then run:

rails g rspec:install

CRUD Exercise

Create a file spec/models/user_spec.rb and paste the following content into it:

require 'spec_helper'
describe User do

  it 'can create a user and retrieve it back by ID' do
    u = User.create(:name => "Joe", :email => "joe@example.com")
    User.find(u.id).should == u
  end

  it 'can instantiate a user with new, then save it, and find it back by ID'

  it 'can update an existing user name with "attributes=" and save, then verify it is been written correctly'

  it 'can update an existing user name with "update_attribute" and save, then verify it is been written correctly'

  it 'can count the number of users'

  it 'can destroy a user, then verify it is gone'

end

Verifying destroyed Users

it 'can destroy a user, then verify it is gone' do
  u = User.create(:name => "Joe", :email => "joe@example.com")
  u.destroy
  u.frozen?.should be_true  # == true
end

it 'can destroy a user, then verify it is gone -- FANCY' do
  u = User.create(:name => "Joe", :email => "joe@example.com")
  expect {
    u.destroy  # code that changes something
  }.to change { User.count }.by(-1)
  u.should be_frozen  # any ? method --> be_method
end

it 'can destroy a user, finding a dead user throws exception' do
  u = User.create(:name => "Joe", :email => "joe@example.com")
  u.destroy  # code that changes something

  expect {
    User.find(u.id)
  }.to raise_error(ActiveRecord::RecordNotFound)
end

Bootstrapping a new project (in general)

  • Make sure you're in the correct Ruby and gemset, i.e.

      rvm use 1.9.2@rails3
    
  • Run rails new sample_app -T

  • Add rspec gem to your Gemfile

      group :development, :test do
        gem 'rspec-rails'
      end
    
  • Setup .rvmrc

  • Run rails g rspec:install

Bootstrapping the Rails Test-First Teaching project

  • Make sure you're in the correct Ruby and gemset, i.e.

      rvm use 1.9.2@rails3
    
  • Run rails new sample_app -T

  • Add the tft_rails gem to your Gemfile

      gem 'tft_rails'
    
  • Setup .rvmrc

  • Run rails g chapter07:begin to get started.

Initial commit to git

git init
git add .
git commit -m 'useful comment'

Please fill out our evaluation survey

http://marakana.com/survey/1946.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment