Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Active Support Secrets

Active Support Secrets

Presented as a lightning talk at Nickel City Ruby Conference 2013 on September 21, 2013.

Video is available at http://www.confreaks.com/videos/2852-nickelcityruby2013-lightning-talk-active-support-secrets

F1c4a3bb1606cc4a61711e61e2fe6146?s=128

Prem Sichanugrist

September 21, 2013
Tweet

More Decks by Prem Sichanugrist

Other Decks in Programming

Transcript

  1. Active Support Secrets

  2. Prem Sichanugrist @sikachu /sikachu

  3. None
  4. None
  5. Secrets

  6. None
  7. None
  8. None
  9. String

  10. String#inquiry

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

     #=>  false
  12. String#squish

  13. very_complex_sql  =  <<-­‐SQL    SELECT  *    FROM  posts  

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

     WHERE  state='published' SQL #  =>  "SELECT  *  FROM  posts  WHERE   state='published'"
  15. String#strip_heredoc

  16. content  =  <<-­‐README    Hello        World! README

    #=>  "    Hello\n        World!\n"
  17. class  FancyHello    def  initialize(user)        if  should_say_hello_to?

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

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

    #=>  "Hello\n    World!\n"
  20. String#to_date String#to_datetime String#to_time

  21. 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
  22. Hash

  23. Hash#transform_keys

  24. hash  =  {  foo:  'foo',  bar:  'bar'  } #=>  {:foo=>"foo",

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

  26. Object#in?

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

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

     ... end
  29. Object#presence

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

  31. state  =  if  params[:state].present?    params[:state] end   country  =

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

  33. None
  34. http://guides.rubyonrails.org

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

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

    some stickers to give away