Skip to content

Instantly share code, notes, and snippets.

@avdi
Forked from saturnflyer/gist:1515021
Created December 24, 2011 06:10
Show Gist options
  • Save avdi/1516514 to your computer and use it in GitHub Desktop.
Save avdi/1516514 to your computer and use it in GitHub Desktop.
allowing objects to specify exception types
class Share
def call
list.each do |address|
referral = new_referral(address)
# All else being equal, I'd probably prefer a caller-specified
# fallback strategy
referral.save do
@unsaved << referral
end
end
end
def new_referral(address)
Referral.new(address)
end
end
class Referral
class Invalid < StandardError; end
def save
# ...
# if invalid:
if block_given? then yield else raise Invalid end
end
end
@saturnflyer
Copy link

Interesting approach. I've been playing with this.
It reads like the block should run during the save. Or in other words, save do looks like something that happens during a successful save. But this does nicely remove the explicit reference to the exception class.

@avdi
Copy link
Author

avdi commented Dec 24, 2011 via email

@saturnflyer
Copy link

Thanks for your thoughts on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment