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

Hidden gems in Ruby on Rails

Hidden gems in Ruby on Rails

Have you heard of a web framework called Ruby on Rails? There're a lot of stuff hidden under the hood of Ruby on Rails that you might not know about, and it might be really useful for you even if you aren't creating a web application. Let's not reimplement the wheel, and see what you can do if you include some part of Rails into your Ruby project.

Presented at RubyConf China 2013 on Oct 27, 2013.

Video is available at http://www.infoq.com/cn/presentations/hidden-gems-inside-those-of-Ruby-on-rails

Prem Sichanugrist

October 27, 2013
Tweet

More Decks by Prem Sichanugrist

Other Decks in Programming

Transcript

  1. Rails • Action Mailer • Action Pack • Active Record

    • Active Model • Active Support • Railties
  2. Rails • Action Mailer • Action Pack • Active Record

    • Active Model • Active Support • Railties
  3. Rails • Action Mailer • Action Pack • Active Record

    • Active Model • Active Support • Railties
  4. [master][~/Projects/rails] ls \ > actionpack/lib/action_dispatch/middleware callbacks.rb public_exceptions.rb ssl.rb cookies.rb reloader.rb

    stack.rb debug_exceptions.rb remote_ip.rb static.rb exception_wrapper.rb request_id.rb templates flash.rb session params_parser.rb show_exceptions.rb
  5. class  Person      include  ActiveModel::Model      attr_accesssor  :name,

     :age   end       person  =  Person.new(name:  "Prem",  age:  27)   person.valid?  #=>  true   person.name  #=>  "Prem"
  6. class  Person      include  ActiveModel::Validations        

     attr_accessor  :name,  :age      validates  :name,  presence:  true      validates  :age,  numericality:  true   end   ! ! !
  7. class  Person      include  ActiveModel::Validations        

     attr_accessor  :name,  :age      validates  :name,  presence:  true      validates  :age,  numericality:  true   end   ! person  =  Person.new   person.name  =  ""   person.valid?  #=>  false
  8. very_complex_sql  =  <<-­‐SQL      SELECT  *      FROM

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

     posts      WHERE  state='published'   SQL   ! #  =>  "SELECT  *  FROM  posts  WHERE   state='published'"
  10. content  =  <<-­‐README      Hello        

     World!   README   ! #=>  "    Hello\n        World!\n"
  11. class  FancyHello      def  initialize(user)        

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

     if  should_say_hello_to?  user              content  =  <<-­‐README   Hello      World!   README          end      end   end   ! #=>  "Hello\n    World!\n"
  13. content  =  <<-­‐README.strip_heredoc      Hello        

     World!   README   #=>  "Hello\n    World!\n"
  14. Date.parse('2013-­‐10-­‐27')   #=>  Sun,  27  Oct  2013   '2013-­‐10-­‐27'.to_date  

    #=>  Sun,  27  Oct  2013       DateTime.parse('2013-­‐10-­‐27  09:40:00')   #=>  Sun,  27  Oct  2013  09:40:00  +0000   '2013-­‐10-­‐27  09:40:00'.to_datetime   #=>  Sun,  27  Oct  2013  09:40:00  +0000       Time.parse('09:40:00')   #=>  2013-­‐10-­‐27  09:40:00  +0800   '09:00:00'.to_time   #=>  2013-­‐10-­‐27  09:40:00  +0800
  15. hash  =  {  foo:  'foo',  bar:  'bar'  }   #=>

     {:foo=>"foo",  :bar=>"bar"}   ! hash.transform_keys  {  |key|      key.to_s.capitalize  }   #=>  {"Foo"=>"foo",  "Bar"=>"bar"}
  16. #  say_hello("John",  "Jane",  language:  "zh-­‐CN")   def  say_hello(*names_or_options)    

     if  names_or_options.last.is_a?(Hash)          options  =  names_or_options.pop          names  =  names_or_options      else          options  =  {}          names  =  names_or_options      end          case  options[:language]      #  ...  
  17. #  say_hello("John",  "Jane",  language:  "zh-­‐CN")   def  say_hello(*names)    

     options  =  names.extract_options!            case  options[:language]      #  ...  
  18. %w(1  2  3  4  5  6  7  8  9  10).in_groups(3)

      #  =>  [   #            [1,  2,  3,  4],   #            [5,  6,  7,  nil],   #            [8,  9,  10,  nil]   #        ]
  19. %w(1  2  3  4  5  6  7  8  9  10).in_groups_of(3)

      #  =>  [   #            [1,  2,  3],   #            [4,  5,  6],   #            [7,  8,  9],   #            [10,  nil,  nil]   #        ]  
  20. str  =  nil   str.nil?  #=>  true   str.empty?  #=>

     NoMethodError   str.blank?  #=>  true   str.present?  #=>  false       str  =  ""   str.nil?  #=>  false   str.empty?  #=>  true   str.blank?  #=>  true   str.present?  #=>  false       str  =  "Hello"   str.nil?  #=>  false   str.empty?  #=>  false   str.blank?  #=>  false   str.present?  #=>  true