HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyMinor

Ruby if-statement, possible to shorten it further?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
statementfurthershortenpossibleruby

Problem

Is it possible to shorten this, or to improve the code in any way? I know it's just a simple if-statement, but it's always fun to learn a shorter way of doing things.

if self.citizenship == 'ES'
  @identification_type = IDENTIFICATION_TYPES[0]
elsif self.country == 'ES'
  @identification_type = IDENTIFICATION_TYPES[1]
end

Solution

You could remove a little duplication like this:

@identification_type = if self.citizenship == 'ES'
  IDENTIFICATION_TYPES[0]
elsif self.country == 'ES'
  IDENTIFICATION_TYPES[1]
end

Code Snippets

@identification_type = if self.citizenship == 'ES'
  IDENTIFICATION_TYPES[0]
elsif self.country == 'ES'
  IDENTIFICATION_TYPES[1]
end

Context

StackExchange Code Review Q#7252, answer score: 3

Revisions (0)

No revisions yet.