Skip to content

Instantly share code, notes, and snippets.

@kossnocorp
Last active December 22, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kossnocorp/6491700 to your computer and use it in GitHub Desktop.
Save kossnocorp/6491700 to your computer and use it in GitHub Desktop.
How to refactor methods like this one?
def activity_status
if currently_working?
'working'
elsif worked_recently?
'active'
elsif has_activity?
'inactive'
elsif signed_in_at_least_once?
'no_activity'
else
'never_sign_in'
end
end
@kossnocorp
Copy link
Author

def activity_status
  case true
  when currently_working?
    'working'
  when worked_recently?
    'active'
  when has_activity?
    'inactive'
  when signed_in_at_least_once?
    'no_activity'
  else
    'never_sign_in'
  end
end

@vrybas
Copy link

vrybas commented Sep 9, 2013

@kossnocorp,

def activity_status
  return 'working'      if currently_working?
  return 'active'       if worked_recently?
  return 'inactive'     if has_activity?
  return 'no_activity'  if signed_in_at_least_once?
  'never_sign_in'
end

@p0deje
Copy link

p0deje commented Sep 9, 2013

def activity_status
  case 
  when currently_working?       then 'working'
  when worked_recently?         then 'active'
  when has_activity?            then 'inactive'
  when signed_in_at_least_once? then 'no_activity'
  else 'never_sign_in'
  end
end

@dmitryn
Copy link

dmitryn commented Sep 9, 2013

Although above solutions are good, however i add new style for variability:

def activity_status
  currently_working? && 'working' or
  worked_recently? && 'active' or 
  has_activity? && 'inactive' or
  signed_in_at_least_once? &&'no_activity' or
  'never_sign_in'
end

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