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

The Problem is Your Ruby

j3
September 22, 2011

The Problem is Your Ruby

Many developers dive into writing Rails applications without building a real foundation in Ruby. Instead we focus on the new whiz-bang features of Rails or the hot gem of the week -- missing the awesomeness right in front of us.

Let's talk about Ruby and fill in some of the gaps. This isn't about mind-blowing features and crazy metaprogramming, it's about the fundamentals that are often overlooked in Rails applications.

j3

September 22, 2011
Tweet

More Decks by j3

Other Decks in Technology

Transcript

  1. U g Person Business M Email q Phone First Name

    Last Name Name Thursday, September 22, 11
  2. class Contact < ActiveRecord::Base has_many :phone_numbers has_many :email_addresses end class

    Person < Contact def name [last_name, first_name].join ", " end end class Company < Contact end Thursday, September 22, 11
  3. class Person < ActiveRecord::Base include Contact def name [last_name, first_name].join

    ", " end end class Company < ActiveRecord::Base include Contact end Thursday, September 22, 11
  4. module Contact def self.included(source) do source.send(:has_many, :phone_numbers) source.send(:has_many, :email_addresses) end

    end /app/models/contact.rb Send the message :has_many with parameter :phone_numbers to source Thursday, September 22, 11
  5. class Article < ActiveRecord::Base before_validation :sanitize_text def sanitize_text self.body.gsub!("Ilias", "Trolololol")

    end end class Comment < ActiveRecord::Base before_validation :sanitize_text def sanitize_text self.body.gsub!("Ilias", "Trolololol") end end Thursday, September 22, 11
  6. module TextDecorator module InstanceMethods def sanitize_text(input) input.gsub!("Ilias", "Trolololol") end end

    def self.included(source) source.send(:include, InstanceMethods) end end class Article < ActiveRecord::Base include TextDecorator before_validation :sanitize_text end Thursday, September 22, 11
  7. module TextDecorator def self.sanitize_text(input) input.gsub!("Ilias", "Trolololol") end end /app/lib/text_decorator.rb class

    Article < ActiveRecord::Base before_validation { TextDecorator.sanitize_text(body) } end class Comment < ActiveRecord::Base before_validation { TextDecorator.sanitize_text(body) } end Thursday, September 22, 11
  8. ## Rails 2 Way class Article < ActiveRecord::Base named_scope :published,

    {:conditions => [:published => true]} named_scope :recent, {:limit => 5, :order => "published_at DESC"} end # Usage: Article.published.recent Thursday, September 22, 11
  9. ## Rails 3 Way class Article < ActiveRecord::Base scope :published,

    where(:published => true) scope :recent, limit(5).order("published_at DESC") end Thursday, September 22, 11
  10. ## Rails 3 Way class Article < ActiveRecord::Base scope :published,

    where(:published => true) scope :recent, limit(5).order("published_at DESC") end ## Rails 3 using Class Methods class Article < ActiveRecord::Base def self.published where(:published => true) end def self.recent limit(5).order("published_at DESC") end end Thursday, September 22, 11
  11. class Article < ActiveRecord::Base scope :published, where(:published => true) scope

    :recent, lambda{|max| limit(max).order("published_at DESC")} end Thursday, September 22, 11
  12. class Article < ActiveRecord::Base scope :published, where(:published => true) scope

    :recent, lambda{|max| limit(max).order("published_at DESC")} end class Article < ActiveRecord::Base def self.published where(:published => true) end def self.recent(max) limit(max).order("published_at DESC") end end Thursday, September 22, 11
  13. module Contact module InstanceMethods def photo_filename name.parameterize + ".jpg" end

    end def self.included(source) do source.send(:has_many, :phone_numbers) source.send(:has_many, :email_addresses) source.send(:include, InstanceMethods) end end Normal Ruby Module Thursday, September 22, 11
  14. module Contact extend ActiveSupport::Concern module InstanceMethods def photo_filename name.parameterize +

    ".jpg" end end included do has_many :phone_numbers has_many :email_addresses end end Using ActiveSupport::Concern Thursday, September 22, 11
  15. module Contact module InstanceMethods def photo_filename name.parameterize + ".jpg" end

    end def self.included(source) do source.send(:has_many, :phone_numbers) source.send(:has_many, :email_addresses) source.send(:include, InstanceMethods) end end Normal Ruby Module Thursday, September 22, 11
  16. The Problem is Your Ruby Rails “expertise” is pointless Focus

    on Ruby, OO, design Master collections & Enumerable Wrap and reuse code with Modules Favor clarity over brevity Jeff Casimir / @j3 Thursday, September 22, 11