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.
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:
Follow instructions here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:install_git
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 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
https://www.e-junkie.com/d/?t=89E04444TP8615939&c=2pl8
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
rvm use 1.9.2@rails3 # if you use RVM
gem install rails
rails new first_app
- Set up ssh key
heroku keys add
heroku create <appname>
git push heroku master
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
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 => "[email protected]")
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
it 'can destroy a user, then verify it is gone' do
u = User.create(:name => "Joe", :email => "[email protected]")
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 => "[email protected]")
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 => "[email protected]")
u.destroy # code that changes something
expect {
User.find(u.id)
}.to raise_error(ActiveRecord::RecordNotFound)
end
-
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
-
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 yourGemfile
gem 'tft_rails'
-
Setup
.rvmrc
-
Run
rails g chapter07:begin
to get started.
git init
git add .
git commit -m 'useful comment'