Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Rails validation for Phone

  validates_length_of :phone, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.phone.blank?}
  validates_length_of :fax, :is => 10, :message => 'must be 10 digits, excluding special characters such as spaces and dashes. No extension or country code allowed.', :if => Proc.new{|o| !o.fax.blank?}

Too small for function, too big to be un-DRY

I wanted to execute the same block of code twice in the same procedure, but the block itself I did not feel warranted its own function (this was in a Rails test). So here's a neat trick to keep it DRY:

    
p = Proc.new do
  r1 = Record.find 1
  r2 = Record.find 2
  r3 = Record.find 3
end

p.call

# ... do some useful stuff ...

p.call # do it again!

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS