[RAILS] [Ruby] A program / concept that combines each_with_index and search

I will write it for output. `Use Ruby 2.6.5 installed on macOS Catalina 10.15.6. ``

Thing you want to do#

You want to create a method using ** search ** and ** each_with_index ** that looks for a number and returns the result of what number it is included in.

First of all, I want to have such an array

input = [3, 5, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]

So, I want to make a search that can be used like this


search(12, input)
=>4th

search(7, input)
=>That number is not included

About each_with_index

** each_with_index ** is one of the standard methods built into Ruby. At the same time as iterating the element, you can also show what number the element was processed.


#each_with_How to use index

Array name.each_with_index  do |item, i|

end

** More detailed examples of using each_with_index **


cigarettes = ["MEVIUS", "highlight", "LuckyStrike"]

cigarettes.each_with_index do |item, i|
 puts "#{i}The second cigarette is#{item}is."
end

If you execute this ↑, you will get the following output result.

The 0th cigarette is MEVIUS.
The first cigarette is highlight.
The second cigarette is Lucky Strike.

Now the main subject, how do you do it?

  1. Anyway, define your own ** search ** method.
  2. Prepare two formal arguments. (Tage_i, input) and so on.
  3. Array inputToeach_with_indexの使い方に当てはめて・・・。ブロック変数にも2つ引数To渡しす(なんとなく | i, index |I made it).
  4. If it is included, there is ~, and if it is not included, there is ~, so you should use conditional branch if ...? If you enter i and it is equal to target_num, puts before return will be output. I wonder if I can repeat as many elements as I want to return!
  5. Arrange the array and call one as a trial

array.rb



def search(tage_i, input)
  input.each_with_index do |i, index|
    if i == tage_i
      puts"#{index + 1}Is in the second!"
      return
    end
  end
  puts "That number is not included"
end

input = [3, 5, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]
search(11, input)

result スクリーンショット-2020-08-24-23.42.57.jpg

By the way, let's output the numbers that apply ...

array.rb



def search(tage_i, input)
  input.each_with_index do |i, index|
    if i == tage_i
      puts"#{index + 1}Is in the second!"
      return
    end
  end
  puts "That number is not included"
end

input = [3, 5, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]
search(11, input)
search(29, input) #Addition of numbers that meet the conditions of "~ th!"

スクリーンショット-2020-08-24-23.42.57.jpg

Anyway

Can you think slowly and carefully ...? I can't deny that I can only understand the surface, so I'll take a note. I think I will update it again.

Recommended Posts

[Ruby] A program / concept that combines each_with_index and search
[Ruby] A program that uses search and each_with_index
[Ruby] A program that determines prime numbers
Combination of search and each_with_index
[ruby] Creating a program that responds only to specific conditions
Ruby: I made a FizzBuzz program!
I wrote a route search program in TDD and refactored it
A program that calculates factorials from 2 to 100
A story that separates business logic and model
Ruby sum is a sober and amazing story
Janken program using Ruby and its test (test-unit)
[Ruby] I want to make a program that displays today's day of the week!
I want to use PowerMock in a class that combines parameterized tests and ordinary tests