Home > Back-end >  What is the better way of writing multiple if statements in Ruby Rails?
What is the better way of writing multiple if statements in Ruby Rails?

Time:11-11

I'm pretty new to Ruby and have this scenario of writing multiple if statements. I want to optimize it and trying to find out if there are any other alternatives or better ways of writing the following conditions -

              if region == "uk"
                value = 1
              end
              if region == "us"
                value = 2
              end
              if region == "ind"
                value = 3
              end
              if region == "cn"
                 value = 4
              end

Problem statement - I can't be going on and on if other regions add up to my scenario. Any optimized way of writing the above code ?

Any teachings and solution would be really appreciated!

CodePudding user response:

One way is to use a case statement:

value = case region
        when 'uk'
          1
        when 'us'
          2
        # etc.
        end

But when you've got a simple list like this with no logic beyond the lookup, you can build a hash with the value to lookup as the key, and the returned value as the value:

region_values = {
  'uk' => 1,
  'us' => 2,
  # etc.
}

value = region_values['uk']
  

CodePudding user response:

There are few ways.

You can also wrap-up if statement in one liner instead of the block

value = 1 if region == "uk"
value = 2 if region == "us"
value = 3 if region == "ind"
value = 4 if region == "cn"

Can also do something like this

value = if region == "uk"
         1
        elsif region == "us"
         2
        elsif region == "ind"
         3
        elsif region == "cn"
         4
        end

But when comparing 2 values, better to use switch statement ie

value = case region
        when "uk"
         1
        when "us"
         2
        when "ind"
         3
        when "cn"
         4
        end

CodePudding user response:

The following are three options I would consider. Initially, I assume that the value of region is known to be one of the values in the array

REGIONS = ["uk", "us", "ind", "cn"]

Suppose

region = "ind"

#1

value =
case region
when "uk"  then 1
when "us"  then 2
when "ind" then 3
when "cn"  then 4
end
  #=> 3

#2

REGION_TO_ID = { "uk"=>1, "us"=>2, "ind"=>3, "cn"=>4 }
value = REGION_TO_ID[region]
  #=> 3

#3

value = REGIONS.index(region)   1
  #=> 3

where REGIONS is as defined above.


If the value of region is not known to be one of the values in the array REGIONS one might first execute the following:

region = "moon"

raise ArgumentError, "'#{region}' is not an element of REGIONS" unless
  REGIONS.include?(region)
  #=> ArgumentError: 'moon' is not an element of REGIONS

Alternatively, in #1 at the end of the case statement one could insert

else raise ArgumentError, "'#{region}' is not an element of REGIONS" 

and in #2 one could add

REGION_TO_ID.default_proc = proc { |_,region|
 raise ArgumentError, "'#{region}' is not an element of REGIONS" }

Then,

REGION_TO_ID['moon']
  #=> ArgumentError: 'moon' is not an element of REGIONS

See Hash#default_proc=.

  • Related