Using guard clause in ruby on rails for multiple independent if clause

446 views Asked by At

How to use the guard clause in the following scenario? The msg is capturing info in 2 independent if-clauses.

def edible?(food_object)

    edible_type = ['fruit','vegetable','nuts']
    food_list  = ['apple','banana','orange','olive','cashew','spinach']

    food = food_object.food
    type = food_object.type
   
   msg = ''
   if edible_type.include?(type)
     msg += 'Edible : '
   end

   if food_list.include?(food)
     msg += 'Great Choice !'
   end

end
2

There are 2 answers

3
wscourge On BEST ANSWER

Like this:

def edible?(food_object)
  edible_type = ['fruit','vegetable','nuts']
  food_list  = ['apple','banana','orange','olive','cashew','spinach']
  food = food_object.food
  type = food_object.type
  
  msg = ''
  msg += 'Edible : ' if edible_type.include?(type)
  msg += 'Great Choice !' if food_list.include?(food)
end

or to return as early as possible

def edible?(food_object)
  edible_type = ['fruit','vegetable','nuts']
  food_list  = ['apple','banana','orange','olive','cashew','spinach']
  food = food_list.include?(food)
  type = edible_type.include?(type)
  msg = ''
  return msg unless food || edible
  msg += 'Edible : ' if type
  msg += 'Great Choice !' if food
end

Side note: Beware that the commonly accepted practice is for the ruby method names to end with ? when they return boolean value.

0
Cary Swoveland On

I suggest the following.

EDIBLE_TYPE = ['fruit','vegetable','nuts']
FOOD_LIST   = ['apple','banana','orange','olive','cashew','spinach']
def edible?(food_object)
  "%s%s" % [EDIBLE_TYPE.include?(food_object.type) ? 'Edible : ' : '',
            FOOD_LIST.include?(food_object.food)   ? 'Great Choice !' : '']
end

We can test this by modifying the method slightly.

def edible?(type, food)
  "%s%s" % [EDIBLE_TYPE.include?(type) ? 'Edible : ' : '',
            FOOD_LIST.include?(food)   ? 'Great Choice !' : '']
end
edible?('nuts', 'olive')     #=> "Edible : Great Choice !" 
edible?('nuts', 'kumquat')   #=> "Edible : " 
edible?('snacks', 'olive')   #=> "Great Choice !" 
edible?('snacks', 'kumquat') #=> "" 

The operative line of the method could alternatively be written:

format("%s%s", EDIBLE_TYPE.include?(food_object.type) ? 'Edible : ' : '',
               FOOD_LIST.include?(food_object.food)   ? 'Great Choice !' : ''])

or

"#{EDIBLE_TYPE.include?(food_object.type) ? 'Edible : ' : ''}#{FOOD_LIST.include?(food_object.food) ? 'Great Choice !' : ''}"

See Kernel#format.