how to display a nil which is in the arguments of a method?

Viewed 48

this is my first post here, I'am a super beginner and I want to solve an exercise.

Well, I want to know how to display a nil which is in the arguments of a method. Here is my code that I tried, but the expected result does not meet my expectations, can you help me? Thank you.

def who_is_bigger(a, b, c)
  max_number = 84
  the_nil = c
  if max_number == a
    puts "a is bigger"
  elsif max_number == b
    puts "b is bigger"
  elsif max_number == c
    puts "c is bigger"
  elsif the_nil == nil
    puts "nil detected"
  else
    puts "nil detected"
  end
end
  
puts who_is_bigger(84, 42, nil)
puts who_is_bigger(nil, 42, 21)
puts who_is_bigger(84, 42, 21)
puts who_is_bigger(42, 84, 21)
puts who_is_bigger(42, 21, 84)

And my terminal return this;

a is bigger
nil detected
a is bigger
b is bigger
c is bigger

But I would like this;

nil detected
nil detected
a is bigger
b is bigger
c is bigger
1 Answers

You need to do your nil check in the beginning before the other conditions.

def who_is_bigger(a, b, c)
  if [a, b, c].include?(nil)
    puts "nil detected"
    return
  end
  max_number = 84
  if max_number == a
    puts "a is bigger"
  elsif max_number == b
    puts "b is bigger"
  elsif max_number == c
    puts "c is bigger"
  else
    puts "nil detected" # this should really be max_number not found
  end
end
Related