Comparing a string with an array of strings and printing the count

I’m comparing a string to an array of strings using:

arry.include? (mystring)

and it returns a boolean value. How can I print out how many strings in that array equaled to mystring?

  • Rails 3 - Validation for Time existence
  • Windows 7: Could not find gem 'typhoeus (~> 0.8.0) x64-mingw32'
  • When I ran `bundle exec rake test:prepare` it errored out, but `bundle exec rake db:test:prepare` goes through fine with warning. What's going on?
  • Do I have to install Ruby on my Apache server to run scripts
  • Heroku error with installing installing sqlite3 (1.3.7)
  • Problems installing wpscan gem on Mac El Capitan
  • Have Devise create a subdomain on registration
  • Mountain Lion install rvm ree-1.8.7
  • 2 Solutions collect form web for “Comparing a string with an array of strings and printing the count”

    array.count(mystring)
    
    ["Cat", "Dog", "Cat"].count("Cat") #=> 2
    
    ["Cat", "Dog", "Cat"].count("Dog") #=> 1
    

    You can use Enumerable#count:

    arry.count { |s| s == mystring }
    

    assuming mystring is a variable holding a string. If it’s meant to be a literal, you want s == "mystring".

    Edit: @kapiltekwani’s answer is clearly better. I forgot about that form of count.

    Ruby is the best programming language in the world - Ruby on Rails.