#9
Case Conditions
>> whichcase(1)
=> "I am an integer."
>> whichcase("1")
=> "I am a string."
>> whichcase(3)
=> "I am either 2 or 3."
>> whichcase(4)
=> "I belong to a range."
>> whichcase(8)
=> "I belong to a range."
>> whichcase(20)
=> "I am 20!"
>> whichcase(:nobody)
=> "I am nobody."
irb
def whichcase(num)
case num
when 1
"I am an integer."
when "1"
"I am a string."
when 2, 3
"I am either 2 or 3."
when 4..10
"I belong to a range."
when ->(x) { x == 20 }
"I am 20!"
else
"I am nobody."
end
end
Slide 25
Slide 25 text
>> %w(hello world).map { |s| s.upcase }
=> ["HELLO", “WORLD"]
!
>> %w(hello world).map(&:upcase)
=> ["HELLO", "WORLD"]
#10
Symbol to Proc to Block
irb
http://stackoverflow.com/questions/14881125/what-does-to-proc-method-mean
Slide 26
Slide 26 text
>> numbers = [1, 2, 3, 4]
=> [1, 2, 3, 4]
!
>> numbers.inject(&:+)
=> 10
#11
Symbol to Proc to Block
irb
Slide 27
Slide 27 text
>> %(hello world).map(&Star)
>> ["** hello **", "** world **"]
#12
Symbol to Proc to Block
irb
class Star
def self.to_proc
lambda { |x| "** #{x} **" }
end
end
#14
Rescue Refactoring
def rescue_exception
boom
rescue NameError => e
puts "Rescued: #{e}"
end
def rescue_exception
begin
boom
rescue Exception => e
puts "Rescued: #{e}"
end
end
#22
Bundler Jobs
$> time bundle install
78.55s user 21.97s system 17% cpu 9:20.32 total
!
$> time bundle install -j4
83.05s user 23.84s system 54% cpu 3:16.18 total
shell
http://bundler.io/v1.5/whats_new.html
#24
Bundler Commands
$> bundle outdated
# List installed gems with newer versions available
!
$> bundle console
# Opens an IRB session with the Gemfile pre-loaded
shell
http://bundler.io/v1.6/commands.html
Slide 42
Slide 42 text
Rails Tips
Slide 43
Slide 43 text
class AddNameToProducts < ActiveRecord::Migration
def change
add_column :products, :name, :string
add_index :products, :name
end
end
Migration Syntax
#25
https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb
$> rails generate migration create_product
AddNameToProducts name:string:index
shell
Slide 44
Slide 44 text
class AddCategoryIdToProducts < ActiveRecord::Migration
def change
add_reference :products, :category_id, index: true
end
end
Migration Syntax
#26
https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb
$> rails generate migration create_product
AddCategoryIdToProducts category_id:references
shell
Slide 45
Slide 45 text
Sandboxed Console
#27
$> rails console —-sandbox
Any modifications you make will be rolled back on exit
!
>> Product.all
=> #
!
>> Product.create(name: “walkman")
(0.0ms) SAVEPOINT active_record_1
SQL (1.7ms) INSERT INTO “products” ...
(0.0ms) RELEASE SAVEPOINT active_record_1
=> #
!
>> exit
(2.1ms) rollback transaction
irb
Slide 46
Slide 46 text
Rake Notes
#28
$> rake notes
app/controllers/application_controller.rb:
* [2] [TODO] remember to do this
* [3] [FIXME] remember to fix this
* [4] [OPTIMIZE] remember to optimize this
irb
class ApplicationController < ActionController::Base
# TODO: remember to do this
# FIXME: remember to fix this
# OPTIMIZE: remember to optimize this
end
Uppercase Only!
cd railsapp; rake -T
Slide 47
Slide 47 text
class UglyText
def self.print
puts <<-HERE
I am not indented.
But I am ugly on the inside.
HERE
end
end
Pretty Heredoc
#29
>> UglyTest.print
I am not indented.
But I am ugly on the inside.
irb
Slide 48
Slide 48 text
class NiceText
def self.print
puts <<-HERE.strip_heredoc
I am not indented.
And I look good on the inside.
HERE
end
end
Pretty Heredoc
#29
>> NiceTest.print
I am not indented.
And I look good on the inside.
irb
Slide 49
Slide 49 text
Question?
#30
require 'active_support'
!
class Winston
cattr_writer :emotion
def self.is
ActiveSupport::StringInquirer.new(@@emotion)
end
end
>> Winston.emotion = "happy"
>> Winston.is.happy?
=> true
irb
Slide 50
Slide 50 text
Sean Xie
Dr. Khoo Chong Yee
Teo Yinquan
Teo Hui Ming
Karthik T
Credits