vendredi 11 décembre 2020

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

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

Aucun commentaire:

Enregistrer un commentaire