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

Rails Magic Exposed

Rails Magic Exposed

The Ruby that powers Rails.. LoneStarRubyConf 2012

Jesse Wolgamott

August 11, 2012

Transcript

40 slides
  1. Framework Magic Total php (1994) 132000 2.527E+10 java (1995) 45900

    960000000 .net (2002) 4400 250000000 django (2004) 9370 44300000 rails (2005) 7040 20300000 Saturday, August 11, 12
  2. 0 1 2 3 4 php (1994) java (1995) .net

    (2002) django (2004) rails (2005) 0.052 0.478 0.176 2.115 3.468 Magic over Time Frequency / 10000 Saturday, August 11, 12
  3. 0 1 2 3 4 php (1994) java (1995) .net

    (2002) django (2004) rails (2005) Node 0.052 0.478 0.176 2.115 3.468 2.031 A Step Back Saturday, August 11, 12
  4. 0 100 200 300 400 php (1994) .net (2002) rails

    (2005) Meteor.js 0.052 0.478 0.176 2.115 3.468 2.031 345.810 MAGIC of Meteor Saturday, August 11, 12
  5. def method_missing(method_id, *arguments) if match = /find_(all_by|by)_([_a-zA-Z]\w*)/. match(method_id.to_s) # find...

    elsif match = /find_or_create_by_([_a-zA-Z] \w*)/. match(method_id.to_s) # find_or_create... else super end end Saturday, August 11, 12
  6. class User < ActiveRecord::Base has_many :posts has_many :comments, through: :posts

    validates_presence_of :email end Saturday, August 11, 12
  7. class SuperWo 20.times {|i| puts "This is the #{i}”} def

    wat! puts "hi" end end SuperWo.new.wat! Try it out: http://rubyfiddle.com/riddles/a63dd Saturday, August 11, 12
  8. # Extremely simple autoloading implementation class Module alias const_missing_without_autoloading const_missing

    def const_missing(const) path = “./#{name.gsub('::', '/')}/#{const}" path.gsub!(/([a-z\d])([A-Z])/,'\1_\2') require path.downcase const_defined?(const) ? const_get(const) : super rescue LoadError => error warn(error.message) super end end Saturday, August 11, 12
  9. class Foo @@foo ||= 42 puts @@foo # 42 end

    # reload... class Foo @@foo ||= 23 puts @@foo # 42 end Saturday, August 11, 12
  10. class Enumerable def standard_dev Math.sqrt(sample_variance) end def sample_variance mean =

    sum.to_f / size.to_f sum{|i| (i-mean)**2} / size end end Saturday, August 11, 12

More Decks by Jesse Wolgamott

Other Decks in Programming