{"id":80017,"date":"2024-03-28T12:01:13","date_gmt":"2024-03-28T03:01:13","guid":{"rendered":"https://freelance.indieverse.co.jp/media/?p=80017"},"modified":"2025-11-01T16:34:43","modified_gmt":"2025-11-01T07:34:43","slug":"ruby-reject","status":"publish","type":"post","link":"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-reject","title":{"rendered":"【Ruby】rejectメソッドの使い方 | reject!やdelete_ifとの違いについても解説"},"content":{"rendered":"<p>この記事では、</p>\n<ul></ul>\n<ul>\n<li>rejectメソッドの基本</li>\n<li>rejectメソッドの活用例</li>\n<li>selectメソッドとの比較と違い</li>\n</ul>\n<p>について解説していきたいと思います。</p>\n\n\t<div class='balloon5'>\n\t\t<div class='faceicon'>\n\t\t\t<img src='https://pbs.twimg.com/profile_images/1230103371664613376/PHLMWlPU_400x400.jpg'>\n\t\t\t<div style='text-align:center'>DAI</div>\n\t\t</div>\n\t\t<div class='chatting'>\n\t\t\t<div class='says'>簡単に説明すると、繰り返し処理をした時に、条件に合致しないものを除くメソッドとなります</div>\n\t\t</div>\n\t</div>\n\t\n<h2>rejectメソッドの基本</h2>\n<p>Rubyにおいて「rejectメソッド」は配列やハッシュから、特定の条件に当てはまる要素を除外するのに使用されます。具体的には、ブロックで定義した条件が<code>true</code>を返す要素を除外し、<code>false</code>を返す要素のみを新しい配列として返します。<br />\nこれを使うことで、不要な要素を効率的に排除できます。</p>\n<p><code></code></p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>numbers = [1, 2, 3, 4, 5]\r\neven_numbers = numbers.reject { |number| number.even? }\r\nputs even_numbers.inspect # =&gt; [1, 3, 5]</code></pre>\n</div>\n<p>このサンプルコードでは、配列<code>numbers</code>から偶数を除外しています。</p>\n<p>ブロック内の<code>number.even?</code>で偶数を判断しており、これが<code>true</code>を返す要素がrejectメソッドにより除外され、結果として奇数のみが<code>even_numbers</code>配列に格納されています。</p>\n\n\t<div class='balloon5'>\n\t\t<div class='faceicon'>\n\t\t\t<img src='https://pbs.twimg.com/profile_images/1230103371664613376/PHLMWlPU_400x400.jpg'>\n\t\t\t<div style='text-align:center'>DAI</div>\n\t\t</div>\n\t\t<div class='chatting'>\n\t\t\t<div class='says'>これを仮にselectで書くと、こうなります。selectの逆バージョンと理解するとよいでしょう。</div>\n\t\t</div>\n\t</div>\n\t\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>numbers = [1, 2, 3, 4, 5]\r\neven_numbers = numbers.select { |number| number.odd? }\r\nputs even_numbers.inspect # =&gt; [1, 3, 5]</code></pre>\n</div>\n<h2>配列での使用例</h2>\n<p>Rubyのプログラミングでは、配列内の要素を操作する機会が多くあります。特に、特定の条件に一致する要素だけを取り除きたい場合に<code>rejectメソッド</code>は役立ちます。</p>\n<p><code></code></p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>animals = ['dog', 'cat', 'elephant', 'bird']\r\nno_birds = animals.reject { |animal| animal == 'bird' }\r\nputs no_birds.inspect # =&gt; [\"dog\", \"cat\", \"elephant\"]</code></pre>\n</div>\n<p>上記の例では、<code>'bird'</code>を配列から除外しています。</p>\n<p>関連：<a href=\"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-array\">Rubyの配列操作完全マニュアル | 追加 / 更新 / 削除 / 繰り返しの方法について現役エンジニアが解説</a></p>\n<a href=\"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-array\" target=\"_blank\" class=\"related-article-container\">\n\t\t\t\t\t<span class=\"related-article__label\">合わせて読みたい</span>\n\t\t\t\t\t<div class=\"related-article__image\" style=\"background-image:url(https://freelance.indieverse.co.jp/media/wp-content/uploads/2024/03/pexels-kevin-ku-577585-150x150.jpg)\"></div>\n\t\t\t\t\t<div class=\"related-article__content\">\n\t\t\t\t\t\t<div class=\"related-article__title\">Rubyの配列操作完全マニュアル | 追加 / 更新 / 削除 / 繰り返しの方法について現役エンジニアが解説</div>\n\t\t\t\t\t\t<div class=\"related-article__description\">Rubyの配列操作を基礎から解説。作成・取得・追加・更新・削除・繰り返しの書き方...</div>\n\t\t\t\t\t</div>\n\t\t\t\t</a>\n<h2>二次元配列での使用例</h2>\n<p>二次元配列においても<code>rejectメソッド</code>は有用です。条件に合わないサブ配列全体を除外できます。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>pairs = [[1, 2], [3, 4], [5, 6]]\r\nno_even_pairs = pairs.reject { |pair| pair[0].even? }\r\nputs no_even_pairs.inspect # =&gt; [[1, 2], [5, 6]]</code></pre>\n</div>\n<p>この例では、サブ配列の最初の要素が偶数であるペアを除外しています。</p>\n<h2>ハッシュでの使用例</h2>\n<p>ハッシュに対しても、<code>rejectメソッド</code>を使用できます。この場合、キーと値のペアに対して条件を適用します。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code>scores = { alice: 50, bob: 60, carol: 70 }\r\nhigh_scores = scores.reject { |_name, score| score &lt; 60 }\r\nputs high_scores.inspect # =&gt; {:bob=&gt;60, :carol=&gt;70}</code></pre>\n</div>\n<p>50点未満のスコアを持つエントリーを除外しています。</p>\n<p>関連：<a href=\"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-hash\">【Ruby】 hashの使い方完全マニュアル | 追加, 更新, 削除, 高度な利用方法について現役エンジニアが解説します</a></p>\n<h2>selectメソッドとの比較</h2>\n<p><code>rejectメソッド</code>と並び、<code>selectメソッド</code>も配列やハッシュの要素を選択するのによく用いられますが、その動作は反対です。</p>\n<p><code>selectメソッド</code>はブロック内の条件が<code>true</code>を返す要素を選択しますが、<code>rejectメソッド</code>は<code>true</code>を返す要素を除外します。</p>\n<p>関連：<a href=\"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-select\">【Ruby】 selectメソッド完全マニュアル | 基礎から応用・filterとの違いについても解説</a></p>\n\n<h2>rejectを破壊的に利用する: delete_ifとの違いについて</h2>\n<p><code>rejectメソッド</code>は非破壊的なメソッドです。つまり、元の配列やハッシュを変更せずに新しいオブジェクトを返します。</p>\n<p>しかし、破壊的な操作が必要な場合、<code>delete_ifメソッド</code>が利用できます。このメソッドは元の配列やハッシュを直接変更し、条件にマッチする要素を削除します。</p>\n<p>破壊的な操作と非破壊的な操作の選択はプログラムの設計に大きく影響しますので、それぞれのメソッドの特性を理解し、適切に使い分けることが重要です。</p>\n<h2>rejectとreject!との違いについて</h2>\n<p><code>reject!</code>  は、rejectの破壊的バージョンです。よって、delete_ifと同じメソッド（エイリアス）となります。</p>\n<p><code></code>以下に`reject!`メソッドの使用方法を示すサンプルコードを紹介します。</p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-ruby\" data-lang=\"Ruby\"><code># 文字列の配列から特定の文字列を削除する例\r\nwords = [\"apple\", \"banana\", \"cherry\", \"date\"]\r\nwords.reject! { |word| word.start_with?(\"b\") }\r\nputs words.inspect\r\n# 出力: [\"apple\", \"cherry\", \"date\"]</code></pre>\n</div>\n<p>関連：<a href=\"https://freelance.indieverse.co.jp/media/programming/ruby/ruby-reject\">【Ruby】rejectメソッドの使い方 | reject!やdelete_ifとの違いについても解説</a></p>\n<h2>参考文献</h2>\n<ul>\n<li><a href=\"https://docs.ruby-lang.org/ja/latest/method/Hash/i/reject=21.html\">instance method Hash#delete_if</a></li>\n<li><a href=\"https://docs.ruby-lang.org/ja/latest/method/Enumerable/i/reject.html\">instance method Enumerable#reject</a></li>\n</ul>\n","protected":false},"excerpt":{"rendered":"<p>Rubyのrejectメソッドの基本と配列・ハッシュでの活用例、selectとの違い、reject!やdelete_ifの使い分けと非破壊・破壊的操作の違いもコード例で丁寧に解説します。</p>\n","protected":false},"author":105,"featured_media":79964,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[190],"tags":[286],"class_list":["post-80017","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby","tag-ruby"],"aioseo_notices":[],"meta_description":"Rubyのrejectメソッドの基本と配列・ハッシュでの活用例、selectとの違い、reject!やdelete_ifの使い分けと非破壊・破壊的操作の違いもコード例で丁寧に解説します。","_links":{"self":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/80017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts"}],"about":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/types/post"}],"author":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/users/105"}],"replies":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/comments?post=80017"}],"version-history":[{"count":5,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/80017/revisions"}],"predecessor-version":[{"id":93446,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/posts/80017/revisions/93446"}],"wp:featuredmedia":[{"embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/media/79964"}],"wp:attachment":[{"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/media?parent=80017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/categories?post=80017"},{"taxonomy":"post_tag","embeddable":true,"href":"https://freelance.indieverse.co.jp/media/wp-json/wp/v2/tags?post=80017"}],"curies":[{"name":"wp","href":"https://api.w.org/{rel}","templated":true}]}}