Slide 1

Slide 1 text

Active Support Secrets

Slide 2

Slide 2 text

Prem Sichanugrist @sikachu /sikachu

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Secrets

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

String

Slide 10

Slide 10 text

String#inquiry

Slide 11

Slide 11 text

today  =  'saturday' today  =  today.inquiry today.saturday?  #=>  true today.sunday?  #=>  false

Slide 12

Slide 12 text

String#squish

Slide 13

Slide 13 text

very_complex_sql  =  <<-­‐SQL    SELECT  *    FROM  posts    WHERE  state='published' SQL #  =>  "    SELECT  *\n    FROM  posts\n     WHERE  state='published'\n"

Slide 14

Slide 14 text

very_complex_sql  =  <<-­‐SQL.squish    SELECT  *    FROM  posts    WHERE  state='published' SQL #  =>  "SELECT  *  FROM  posts  WHERE   state='published'"

Slide 15

Slide 15 text

String#strip_heredoc

Slide 16

Slide 16 text

content  =  <<-­‐README    Hello        World! README #=>  "    Hello\n        World!\n"

Slide 17

Slide 17 text

class  FancyHello    def  initialize(user)        if  should_say_hello_to?  user            content  =  <<-­‐README                Hello                    World!            README        end    end end #=>  "                Hello\n                    World!\

Slide 18

Slide 18 text

class  FancyHello    def  initialize(user)        if  should_say_hello_to?  user            content  =  <<-­‐README Hello    World! README        end    end end #=>  "Hello\n    World!\n"

Slide 19

Slide 19 text

content  =  <<-­‐README.strip_heredoc    Hello        World! README #=>  "Hello\n    World!\n"

Slide 20

Slide 20 text

String#to_date String#to_datetime String#to_time

Slide 21

Slide 21 text

Date.parse('2013-­‐09-­‐21') #=>  Sat,  21  Sep  2013 '2013-­‐09-­‐21'.to_date #=>  Sat,  21  Sep  2013   DateTime.parse('2013-­‐09-­‐21  09:00:00') #=>  Sat,  21  Sep  2013  09:00:00  +0000 '2013-­‐09-­‐21  09:00:00'.to_datetime #=>  Sat,  21  Sep  2013  09:00:00  +0000   Time.parse('09:00:00') #=>  2013-­‐09-­‐21  09:00:00  -­‐0400 '09:00:00'.to_time #=>  2013-­‐09-­‐21  09:00:00  -­‐0400

Slide 22

Slide 22 text

Hash

Slide 23

Slide 23 text

Hash#transform_keys

Slide 24

Slide 24 text

hash  =  {  foo:  'foo',  bar:  'bar'  } #=>  {:foo=>"foo",  :bar=>"bar"} hash.transform_keys  {  |key|    key.to_s.capitalize  } #=>  {"Foo"=>"foo",  "Bar"=>"bar"}

Slide 25

Slide 25 text

Object

Slide 26

Slide 26 text

Object#in?

Slide 27

Slide 27 text

STARTING_POKEMONS  =  %w(Chespin  Fennekin  Froakie)   if  STARTING_POKEMONS.include?(current_pokemon)    #  ... end

Slide 28

Slide 28 text

STARTING_POKEMONS  =  %w(Chespin  Fennekin  Froakie)   if  current_pokemon.in?(STARTING_POKEMONS)    #  ... end

Slide 29

Slide 29 text

Object#presence

Slide 30

Slide 30 text

"Hello".presence #=>  "Hello" nil.presence #=>  nil

Slide 31

Slide 31 text

state  =  if  params[:state].present?    params[:state] end   country  =  if  params[:country].present?    params[:country] end   region  =  state  ||  country  ||  'US'

Slide 32

Slide 32 text

region  =  params[:state].presence  ||    params[:country].presence  ||  'US'

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

http://guides.rubyonrails.org

Slide 35

Slide 35 text

Thank you! Prem Sichanugrist (@sikachu) Sides: http://bit.ly/as-secret

Slide 36

Slide 36 text

Thank you! Prem Sichanugrist (@sikachu) Sides: http://bit.ly/as-secret I also have some stickers to give away