Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
DZone 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

Garry has posted 5 posts at DZone. View Full User Profile

Too Small For Function, Too Big To Be Un-DRY

08.30.2006
Email
Views: 2087
  • submit to reddit
        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!

    

Comments

Snippets Manager replied on Sat, 2006/09/09 - 4:19pm

But I want to mention, that such usage of blocks is a real memory leak.