Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Literals should be allowed in case statements #889

Closed
sferik opened this issue Mar 15, 2014 · 1 comment
Closed

Literals should be allowed in case statements #889

sferik opened this issue Mar 15, 2014 · 1 comment

Comments

@sferik
Copy link
Contributor

sferik commented Mar 15, 2014

IMHO, literals should be allowed in case statements. They were disallowed in 56fa5fc.

Consider a case statement that is conditional on two variables. For example, here is a method that geocodes an address, latitude/longitude pair, or US zip code, depending on the number of arguments specified and their type.

def extract_location(args)
  case [args.size, args.first.class]
  when [1, Integer]
    {zip: to_zip_code(args[0])}
  when [1, String]
    place = Geocoder.search(args[0]).first
    {latitude: place.latitude, longitude: place.longitude}
  when [2, Numeric]
    {latitude: args[1], longitude: args[1]}
  else
    fail(ArgumentError, 'Must pass a latitude/longitude, zip or address')
  end
end

Obviously, this code could be rewritten as as nested conditional statements but that would require duplicating the else clause. I think this a valid use case for a literal in a case statement.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 15, 2014

Yeah, this totally slipped my mind. Obviously not all literals make sense as a condition (therefore the change I made), though. I'll update this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants