Papouche Guinslyzinho
Papouche Guinslyzinho

Reputation: 5458

Ruby function with an if statement

I have this following function but somehow it doesn't get inside the if statement. This is my gist on line 68

I'm trying to evaluate if an object.country name has the same name as "example_of_a_country_name"

def find_population_of_country(liste_pays, country_name)
  given_country_population = 0
  liste_pays.each do |n| 
    if (n.country.eql?(country_name.upcase))
      given_country_population = n.population

      #I'm trying to see if it's output something here
      puts country_name.upcase
    end
  return given_country_population
  end
end

Upvotes: 0

Views: 55

Answers (2)

jeremycole
jeremycole

Reputation: 2761

The problem is your line:

return given_country_population

This should be moved one line up, inside the if ... end block for the name comparison, otherwise, it's returning even for non-matches.

Upvotes: 1

Sherwyn Goh
Sherwyn Goh

Reputation: 1352

can you type this within your function and tell me what you get?:

def find_population_of_country(liste_pays, country_name)
  liste_pays.select {|n| n.country.eql?(country_name.upcase) }
 end
end

Upvotes: 1

Related Questions