[Ruby] Method to count specific characters

Overview

When the following problem is raised, I will leave it as a memorandum because it was wrong. **【problem】 Create a method that counts how many arbitrary strings exist and outputs that number **

My answer

I have answered the following.

Ruby


def count_hi(word)
  puts word.count('hi')
end

word = "hi!hideyuki. hidemi."
count_hi(word)

Originally, "3" is the correct answer, but in the above description, it becomes "8": expressionless:

Correct answer

Ruby


def count_hi(word)
  puts word.scan('hi').length
end

word = "hi!hideyuki. hidemi."
count_hi(word)

In the above description, "8" is displayed: relaxed:

Commentary

** count ** counts the number of "h" and "i" instead of "hi".

** scan ** returns an array of matched substrings. Therefore, if there is no ** length **, the display will be as follows.

Ruby


puts word.scah('hi')
#hi hi hi is displayed

** length ** originally returns the number of characters in the string, but by setting it to word.scan ('hi'). ** length **, the array returned by scan will be counted.

reference

Counting the number of occurrences of a specific character --Ruby Tips! String # count (Ruby 2.7.0 Reference Manual) --Ruby-lang String # scan (Ruby 2.7.0 Reference Manual) --Ruby-lang String # length (Ruby 2.7.0 Reference Manual) --Ruby-lang

Recommended Posts

[Ruby] Method to count specific characters
[Ruby] How to use any? Method
How to use Ruby inject method
[Ruby] From the basics to the inject method
Ruby length, size, count How to use
Extract characters from Ruby strings slice method
Introduction to Ruby 2
Ruby to_s method
Method to search
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
[Ruby] initialize method
Ruby build method
Ruby accessor method
ruby map method
[Ruby] How to use gsub method and sub method
[Ruby] I want to do a method jump!
[Ruby on Rails] How to use session method
[Ruby] When you want to replace multiple characters
[Ruby basics] How to use the slice method
Method summary to update multiple columns [Ruby on Rails]
Ruby Learning # 30 Initialize Method
Class to take count
abbreviation for ruby method
Ruby Learning # 24 Exponent Method
How to add characters to display when using link_to method
[Ruby] Method to easily get the receiver type .class
Ruby Thread # [] = method notes
definition of ruby method
From Java to Ruby !!
Ruby Regular Expression Extracts from a specific string to a string
[Ruby] Method definition summary
16 Corresponds to method invocation
[Ruby] Count an even number in an array using the even? Method
I want to call a method and count the number
I'm glad to have a method to blink characters on Android
Apply CSS to a specific View in Ruby on Rails
[ruby] Creating a program that responds only to specific conditions
I want to convert characters ...
How to use Ruby return
Convert to Ruby Leet string
Integer check method with ruby
Ruby algorithm (inject, method definition)
[Ruby] How to comment out
[Ruby] Introduction to Ruby Error statement
Rbenv command to use Ruby
Ruby: How to use cookies
20 Corresponds to static method invocation
[Ruby] Notes on gets method
[Ruby] How to write blocks
[Ruby] Method that returns truth
Easy to use array.map (&: method)
How to create a method
[ruby] Method call with argument
[Ruby] How to count even or odd numbers in an array
[Ruby on Rails] Use the resources method to automatically create routes.
Easy to understand the difference between Ruby instance method and class method.