10. Variants
class ApplicationController < ActionController::Base
before_action :detect_variant
private
def detect_variant
request.variant =
case request.user_agent
when /iPhone/ then :phone
when /iPad/
then :tablet
end
end
end
respond_to do |format|
format.html do |html|
html.phone
# index.html+phone.erb
html.tablet do # index.html+tablet.erb
# ...
render
end
end
end
19. TIME TRAVEL
test "Fast forward to Christmas" do
refute is_it_christmas_yet?
travel_to Time.new(2013, 12, 25, 00, 00, 00)
assert is_it_christmas_yet?
end
test "How I met your mother" do
@mother = User.find(...)
travel -30.years do
assert @mother.single?
end
refute @mother.single?
end
23. spring
$ time ./bin/rails runner 'puts "Hello"'
Hello
0.05s user 0.02s system 1% cpu 3.860 total
$ time ./bin/rails runner 'puts "Hello"'
Hello
0.05s user 0.01s system 27% cpu 0.229 total
$ time ./bin/rake spec
.............................................................................................
Finished in 15.04 seconds
322 examples, 0 failures
0.06s user 0.02s system 0% cpu 25.446 total
$ time ./bin/rake spec
.............................................................................................
Finished in 14.79 seconds
322 examples, 0 failures
0.05s user 0.01s system 0% cpu 20.543 total
30. String#Remove
>> ' a lot of
whitespace
=> "alotofwhitespace"
'.remove(/[:space:]/)
>> 'username=godfrey&password=123456'.remove(/&?password=[^&]*/)
=> "username=godfrey"