0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

It would also help if you could specify how the models are related.
thks Sherwyn goh! First now this function works. I'll be able to continue. Second: It's not a model. I'm trying to learn classes in Ruby by redoing all the assigment that I had in Java. Thks!
ahh right, my bad didn't see the lack of a 'rails' tag. Happy it works, have fun coding.
1

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.

1 Comment

thks @jeremycole! I was thinking how .select works but not .each ??? thanks for the tip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.