0

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
wscourge
  • 10,657
  • 14
  • 59
  • 80
Vinit Kumar
  • 55
  • 10

2 Answers2

2

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.

wscourge
  • 10,657
  • 14
  • 59
  • 80
  • 1
    It worked. Thanks a lot for the code and the suggestion ! – Vinit Kumar Dec 11 '20 at 16:37
  • 1
    Your solution will return `nil` if `food_list.include?(food) == false`. Also seems that "Edible: " is a strange response so it might make more sense to be `edible_type.include?(type) && food_list.include?(food) ? "Edible: Great Choice!" : ""` – engineersmnky Dec 11 '20 at 18:06
  • You've apparently seen @engineersmnky's comment, so why haven't you corrected your code? – Cary Swoveland Dec 11 '20 at 23:25
1

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.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100