Skip to content

Instantly share code, notes, and snippets.

@carlossanchezp
Created April 2, 2014 21:16
Show Gist options
  • Save carlossanchezp/9943349 to your computer and use it in GitHub Desktop.
Save carlossanchezp/9943349 to your computer and use it in GitHub Desktop.
The best way to do with or without IF:
This way with if
S.find(:all).each do |each|
if each.expired?
each.renew!
else
each.update!
end
end
OR this one wotout if:
s = S.find :all
expired = s.select {|each| each.expired?}
expired.each {|each| each.renew!}
active = subscriptions.reject {|each| each.expired?}
active.each {|each| each.update!}
@ciscou
Copy link

ciscou commented Apr 2, 2014

active, expired = S.find(:all).partition(&:active?)
active.each &:update!
expired.each &:renew!

@ciscou
Copy link

ciscou commented Apr 2, 2014

first one or my comment above. But please please do not call the block parameter "each". Call it "e" (element) or something more semantic ("subscription")

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