Skip to content

Instantly share code, notes, and snippets.

@joho
Forked from xaviershay/with.rb
Created November 18, 2008 05:47
Show Gist options
  • Save joho/26065 to your computer and use it in GitHub Desktop.
Save joho/26065 to your computer and use it in GitHub Desktop.
# DRY temporary variables that require single use only, without assignment... seems
# useful for configuration files perhaps? urgh.
class Object
def with(object)
yield(object)
end
end
@reports = []
with 'Sales' do |group|
@reports << Report.new(:name => 'First', :group => group)
@reports << Report.new(:name => 'Second', :group => group)
@reports << Report.new(:name => 'Third', :group => group)
end
# this is less typing.
begin
group = 'Traffic'
@reports << Report.new(:name => 'Fourth', :group => group)
@reports << Report.new(:name => 'Fifth', :group => group)
@reports << Report.new(:name => 'Sixth', :group => group)
end
puts group # => 'Traffic' # => FAIL
# oops, is a ruby 1.9 thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment