【Ruby】each_with_index와 search를 조합한 프로그램·사고방식
macOS Catalina10.15.6にインストールしたRuby 2.6.5を使用。하고 싶은 일
숫자를 찾고 몇 번째에 포함되어 있는지 결과를 반환하는 메서드를 search와 each_with_index를 사용하여 만들고 싶지요.
 우선 이런 배열이 있고 싶다
input = [3, 5, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]
 그래서 이런 느낌으로 사용할 수있는 search로하고 싶다.
search(12, input)
=> 4番目にあります
search(7, input)
=> その数は含まれていません
 each_with_index 정보
each_with_index는 Ruby에 표준으로 내장되어있는 메소드 중 하나입니다. 요소의 반복 처리와 동시에, 그 요소가 몇번째로 처리된 것도 나타낼 수 있어요.
#each_with_indexの使い方
配列名.each_with_index  do |item, i|
end
더 자세한 each_with_index 사용 예
cigarettes = ["MEVIUS", "highlight", "LuckyStrike"]
cigarettes.each_with_index do |item, i|
 puts "#{i}番目のタバコは、#{item}です。"
end
이것 ↑을 실행하면, 이하와 같은 출력 결과가 얻어지면 된다.
0番目のタバコは、MEVIUSです。
1番目のタバコは、highlightです。
2番目のタバコは、LuckyStrikeです。
 자, 주제, 어떻게?
input = [3, 5, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]
search(12, input)
=> 4番目にあります
search(7, input)
=> その数は含まれていません
#each_with_indexの使い方
配列名.each_with_index  do |item, i|
end
cigarettes = ["MEVIUS", "highlight", "LuckyStrike"]
cigarettes.each_with_index do |item, i|
 puts "#{i}番目のタバコは、#{item}です。"
end
0番目のタバコは、MEVIUSです。
1番目のタバコは、highlightです。
2番目のタバコは、LuckyStrikeです。
배열 input 을 each_with_index 의 사용법에 맞추고・・・. 블록 변수에도 두 개의 인수를 전달합니다 (어쨌든 | i, index |로 설정).
array.rb
def search(tage_i, input)
  input.each_with_index do |i, index|
    if i == tage_i
      puts"#{index + 1}番目にあります!"
      return
    end
  end
  puts "その数は含まれていません"
end
input = [3, 5, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]
search(11, input)
결과

덧붙여서 맞는 숫자도 출력시켜 본다・・・
array.rb
def search(tage_i, input)
  input.each_with_index do |i, index|
    if i == tage_i
      puts"#{index + 1}番目にあります!"
      return
    end
  end
  puts "その数は含まれていません"
end
input = [3, 5, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]
search(11, input)
search(29, input) #「〜番目にあります!」の条件にあてはまる数字の追加

어쨌든
천천히 차분히 생각하면 된다···?
아직 표면만 밖에 이해할 수 없는 감부할 수 없기 때문에, 일단 메모한다. 또 업데이트한다고 생각한다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(【Ruby】each_with_index와 search를 조합한 프로그램·사고방식), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/YsDMei/items/4f82683484e6f4931bea
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                 우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
Reference
이 문제에 관하여(【Ruby】each_with_index와 search를 조합한 프로그램·사고방식), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/YsDMei/items/4f82683484e6f4931bea텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)