{"id":40509,"date":"2025-12-05T08:07:53","date_gmt":"2025-12-05T08:07:53","guid":{"rendered":"https:\/\/www.railscarma.com\/?p=40509"},"modified":"2025-12-05T08:07:56","modified_gmt":"2025-12-05T08:07:56","slug":"ruby-inject-and-ruby-reduce","status":"publish","type":"post","link":"https:\/\/www.railscarma.com\/fr\/blog\/ruby-inject-and-ruby-reduce\/","title":{"rendered":"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration"},"content":{"rendered":"<div data-elementor-type=\"wp-post\" data-elementor-id=\"40509\" class=\"elementor elementor-40509\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-2514ba8 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"2514ba8\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-905c8c8\" data-id=\"905c8c8\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-047eaf2 elementor-widget elementor-widget-text-editor\" data-id=\"047eaf2\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><span style=\"font-weight: 400;\">Ruby's <\/span><span style=\"font-weight: 400;\"><code>Enumerable<\/code><\/span><span style=\"font-weight: 400;\"> module provides a suite of methods for working with collections like arrays, hashes, and ranges. Among the most versatile are <\/span><span style=\"font-weight: 400;\"><code>injecter<\/code><\/span><span style=\"font-weight: 400;\"> et <\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\">. If you&#8217;re coming from languages like JavaScript or Python, these might remind you of the <\/span><span style=\"font-weight: 400;\"><code>reduce()<\/code><\/span><span style=\"font-weight: 400;\"> function\u2014 and for good reason. In Ruby, <\/span><span style=\"font-weight: 400;\"><code>injecter<\/code><\/span><span style=\"font-weight: 400;\"> et <\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\"> are exact aliases for the same method, meaning they execute identical code with no performance difference. The choice between them often boils down to personal or team preference, readability, or convention.<\/span><\/p><p><span style=\"font-weight: 400;\">In this article, we&#8217;ll dive deep into what these methods do, how they work under the hood, common use cases, pitfalls to avoid, and when to reach for alternatives like <\/span><span style=\"font-weight: 400;\"><code>each_with_object<\/code><\/span><span style=\"font-weight: 400;\">. By the end, you&#8217;ll have a solid grasp of how to &#8220;fold&#8221; your collections into single values or transformed objects efficiently. Whether you&#8217;re a Ruby newbie or a seasoned developer, expect practical examples, visualizations of the iteration process, and tips to make your code more idiomatic.<\/span><\/p><h3><b>What Do Ruby Inject and Ruby Reduce Do?<\/b><\/h3><p><span style=\"font-weight: 400;\">At their core, <\/span><span style=\"font-weight: 400;\"><code>injecter<\/code><\/span><span style=\"font-weight: 400;\"> et <\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span> <i><span style=\"font-weight: 400;\">r\u00e9duire<\/span><\/i><span style=\"font-weight: 400;\"> a collection to a single value by iteratively applying a block (or a symbol like <\/span><span style=\"font-weight: 400;\">:+<\/span><span style=\"font-weight: 400;\"> for addition) across its elements. Think of it as starting with an initial &#8220;accumulator&#8221; (often called <\/span><span style=\"font-weight: 400;\"><code>memo<\/code><\/span><span style=\"font-weight: 400;\">), then combining each element with that accumulator step by step.<\/span><\/p><p><span style=\"font-weight: 400;\">The method signature is flexible:<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">enumerable.inject(initial_value) { |memo, element| ... } <\/span><span style=\"font-weight: 400;\"># or shorthand with a symbol <\/span><span style=\"font-weight: 400;\">enumerable.inject(:+)<\/span><\/pre><ul><li><b>Without an initial value<\/b><span style=\"font-weight: 400;\">: The first element becomes the initial <\/span><span style=\"font-weight: 400;\"><code>memo<\/code><\/span><span style=\"font-weight: 400;\">, and iteration starts from the second element.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>With an initial value<\/b><span style=\"font-weight: 400;\">: Iteration starts from the first element, using your provided value as the starting <\/span><span style=\"font-weight: 400;\"><code>memo<\/code><\/span><span style=\"font-weight: 400;\">.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Symbol shorthand<\/b><span style=\"font-weight: 400;\">: Ruby infers the operation (e.g., <\/span><span style=\"font-weight: 400;\">:+<\/span><span style=\"font-weight: 400;\"> for sum, <\/span><span style=\"font-weight: 400;\">:*<\/span><span style=\"font-weight: 400;\"> for product).<\/span><\/li><\/ul><p><span style=\"font-weight: 400;\">This &#8220;folding&#8221; operation is inspired by functional programming concepts like <\/span><span style=\"font-weight: 400;\"><code>foldl<\/code><\/span><span style=\"font-weight: 400;\"> ou <\/span><span style=\"font-weight: 400;\"><code>foldr<\/code><\/span><span style=\"font-weight: 400;\"> in Haskell, but Ruby&#8217;s implementation is left-associative (processes left to right).<\/span><\/p><h4><b>A Simple Example: Summing an Array<\/b><\/h4><p><span style=\"font-weight: 400;\">Let&#8217;s sum the numbers from 1 to 5:<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">numbers = [1, 2, 3, 4, 5]<\/span> <span style=\"font-weight: 400;\"># Using inject with initial value 0 <\/span><span style=\"font-weight: 400;\">sum = numbers.inject(0) { |memo, num| memo + num } <\/span><span style=\"font-weight: 400;\">puts sum\u00a0 # =&gt; 15<\/span><\/pre><p><b>Visualization of Iteration<\/b><span style=\"font-weight: 400;\"> (step-by-step trace):<\/span><\/p><table><tbody><tr><td><b>Step<\/b><\/td><td><b>Current <\/b><span style=\"font-weight: 400;\"><code>memo<\/code><\/span><\/td><td><b>Current <\/b><span style=\"font-weight: 400;\"><code>num<\/code><\/span><\/td><td><b>Operation<\/b><\/td><td><b>Nouveau <\/b><span style=\"font-weight: 400;\"><code>memo<\/code><\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Start<\/span><\/td><td><span style=\"font-weight: 400;\">0 (initial)<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td><td><span style=\"font-weight: 400;\">0<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">1<\/span><\/td><td><span style=\"font-weight: 400;\">0<\/span><\/td><td><span style=\"font-weight: 400;\">1<\/span><\/td><td><span style=\"font-weight: 400;\">0 + 1<\/span><\/td><td><span style=\"font-weight: 400;\">1<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">2<\/span><\/td><td><span style=\"font-weight: 400;\">1<\/span><\/td><td><span style=\"font-weight: 400;\">2<\/span><\/td><td><span style=\"font-weight: 400;\">1 + 2<\/span><\/td><td><span style=\"font-weight: 400;\">3<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">3<\/span><\/td><td><span style=\"font-weight: 400;\">3<\/span><\/td><td><span style=\"font-weight: 400;\">3<\/span><\/td><td><span style=\"font-weight: 400;\">3 + 3<\/span><\/td><td><span style=\"font-weight: 400;\">6<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">4<\/span><\/td><td><span style=\"font-weight: 400;\">6<\/span><\/td><td><span style=\"font-weight: 400;\">4<\/span><\/td><td><span style=\"font-weight: 400;\">6 + 4<\/span><\/td><td><span style=\"font-weight: 400;\">10<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">5<\/span><\/td><td><span style=\"font-weight: 400;\">10<\/span><\/td><td><span style=\"font-weight: 400;\">5<\/span><\/td><td><span style=\"font-weight: 400;\">10 + 5<\/span><\/td><td><span style=\"font-weight: 400;\">15<\/span><\/td><\/tr><\/tbody><\/table><p><span style=\"font-weight: 400;\">Using the shorthand:<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">sum = numbers.inject(:+)\u00a0 # =&gt; 15 (implicit initial 0 for numerics) <\/span><\/pre><p><span style=\"font-weight: 400;\">Without an initial value (watch the pitfall below!):<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">sum = numbers.inject { |memo, num| memo + num }\u00a0 # Starts with memo=1, skips nothing extra \u2192 15<\/span><\/pre><h3><b>Why Two Names? Ruby <\/b><span style=\"font-weight: 400;\"><code>injecter<\/code><\/span><b> vs. Ruby <\/b><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><\/h3><p><span style=\"font-weight: 400;\">Ruby loves aliases to accommodate developers from diverse backgrounds\u2014<\/span><span style=\"font-weight: 400;\"><code>carte<\/code><\/span><span style=\"font-weight: 400;\"><code>\/<\/code><\/span><span style=\"font-weight: 400;\"><code>collecter<\/code><\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\"><code>d\u00e9tecter<\/code><\/span><span style=\"font-weight: 400;\"><code>\/<\/code><\/span><span style=\"font-weight: 400;\"><code>trouver<\/code><\/span><span style=\"font-weight: 400;\">, and yes, <\/span><span style=\"font-weight: 400;\"><code>injecter<\/code><\/span><span style=\"font-weight: 400;\"><code>\/<\/code><\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\">.<\/span><\/p><ul><li><span style=\"font-weight: 400;\"><code>injecter<\/code><\/span><span style=\"font-weight: 400;\"><code>: <\/code>The original name in Ruby, evoking &#8220;injecting&#8221; the accumulator into each element. It&#8217;s favored by Ruby purists for its whimsical flair and historical precedence.<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\">: Added in Ruby 1.8.7 for familiarity with languages like Python (<\/span><span style=\"font-weight: 400;\"><code>functools.reduce<\/code><\/span><span style=\"font-weight: 400;\">) or JavaScript (Array.prototype.reduce). It&#8217;s more descriptive of the &#8220;reducing to one value&#8221; intent.<\/span><\/li><\/ul><p><span style=\"font-weight: 400;\">From the<\/span><a href=\"https:\/\/ruby-doc.org\/core\/Enumerable.html#method-i-inject\"><span style=\"font-weight: 400;\"> Ruby documentation<\/span><\/a><span style=\"font-weight: 400;\">:<\/span><\/p><p><i><span style=\"font-weight: 400;\">&#8220;Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.&#8221;<\/span><\/i><\/p><p><span style=\"font-weight: 400;\">They&#8217;re interchangeable, but conventions emerge:<\/span><\/p><ul><li><span style=\"font-weight: 400;\">Utilisation <\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\"> for mathematical or data-processing reductions (e.g., sums, products).<\/span><\/li><li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Utilisation <\/span><span style=\"font-weight: 400;\"><code>injecter<\/code><\/span><span style=\"font-weight: 400;\"> for building structures (e.g., hashes from arrays).<\/span><\/li><\/ul><p><span style=\"font-weight: 400;\">In practice:<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">[1, 2, 3].reduce(:+)\u00a0 \u00a0 # Mathematical vibe <\/span><span style=\"font-weight: 400;\">[1, 2, 3].inject(0, :+) # Building from zero<\/span><\/pre><p><span style=\"font-weight: 400;\">A<\/span><a href=\"https:\/\/stackoverflow.com\/questions\/13813243\/is-inject-the-same-thing-as-reduce-in-ruby\"><span style=\"font-weight: 400;\"> Stack Overflow discussion<\/span><\/a><span style=\"font-weight: 400;\"> notes: &#8220;Some programmers use <\/span><span style=\"font-weight: 400;\"><code>injecter<\/code><\/span><span style=\"font-weight: 400;\"> for blocks and <\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\"> for procs\/symbols, but it&#8217;s purely semantic.&#8221;<\/span><\/p><h3><b>Common Use Cases<\/b><\/h3><h4><strong>1 . Basic Aggregations<\/strong><\/h4><ul><li><b>Sum or Product<\/b><span style=\"font-weight: 400;\">:<\/span><\/li><\/ul><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">prices = [10.99, 20.50, 15.00]<\/span> <span style=\"font-weight: 400;\">total = prices.reduce(0) { |sum, price| sum + price * 1.08 }\u00a0 # With tax \u2192 50.18<\/span><\/pre><ul><li style=\"font-weight: 400;\" aria-level=\"1\"><b>Flattening Nested Arrays<\/b><span style=\"font-weight: 400;\">:<\/span><\/li><\/ul><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">nested = [[1, 2], [3, 4], [5]]<\/span> <span style=\"font-weight: 400;\">flat = nested.inject([]) { |memo, arr| memo + arr }\u00a0 # =&gt; [1, 2, 3, 4, 5]<\/span> <span style=\"font-weight: 400;\"># Or shorthand: nested.reduce(:+)\u00a0 # But careful with non-array elements!<\/span><\/pre><h4><strong><span style=\"font-style: inherit;\">2. <\/span>Building Data Structures<\/strong><\/h4><p><span style=\"font-weight: 400;\">Convert an array of pairs to a hash:<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">pairs = [['apple', 1], ['banana', 2]] <\/span><span style=\"font-weight: 400;\">hash = pairs.inject({}) { |memo, (key, value)| memo[key] = value; memo } <\/span><span style=\"font-weight: 400;\"># =&gt; {'apple' =&gt; 1, 'banana' =&gt; 2}<\/span><\/pre><p><b>Iteration Visualization<\/b><span style=\"font-weight: 400;\">:<\/span><\/p><table style=\"font-size: 16px; font-style: normal;\"><tbody><tr><td><b>Step<\/b><\/td><td><span style=\"font-weight: 400;\"><code>memo<\/code><\/span><b> (Hash)<\/b><\/td><td><b>Element (Pair)<\/b><\/td><td><b>Operation<\/b><\/td><td><b>Nouveau <\/b><span style=\"font-weight: 400;\"><code>memo<\/code><\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">Start<\/span><\/td><td><span style=\"font-weight: 400;\">{}<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td><td><span style=\"font-weight: 400;\">&#8211;<\/span><\/td><td><span style=\"font-weight: 400;\">{}<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">1<\/span><\/td><td><span style=\"font-weight: 400;\">{}<\/span><\/td><td><span style=\"font-weight: 400;\">[&#8216;apple&#8217;, 1]<\/span><\/td><td><span style=\"font-weight: 400;\">{}[&#8216;apple&#8217;] = 1<\/span><\/td><td><span style=\"font-weight: 400;\">{&#8216;apple&#8217; =&gt; 1}<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">2<\/span><\/td><td><span style=\"font-weight: 400;\">{&#8216;apple&#8217; =&gt; 1}<\/span><\/td><td><span style=\"font-weight: 400;\">[&#8216;banana&#8217;, 2]<\/span><\/td><td><span style=\"font-weight: 400;\">memo[&#8216;banana&#8217;] = 2<\/span><\/td><td><span style=\"font-weight: 400;\">{&#8216;apple&#8217; =&gt; 1, &#8216;banana&#8217; =&gt; 2}<\/span><\/td><\/tr><\/tbody><\/table><h4><strong>3. Finding the &#8220;Winner&#8221; (Max\/Min by Custom Logic)<\/strong><\/h4><p><span style=\"font-weight: 400;\">Longest word in a sentence:<\/span><\/p><pre><span style=\"font-weight: 400;\">rubis\n<\/span><span style=\"font-weight: 400;\">words = ['Ruby', 'is', 'awesome', 'language']\n<\/span><span style=\"font-weight: 400;\">longest = words.reduce do |memo, word|\n<\/span><span style=\"font-weight: 400;\">    memo.length &gt; word.length ? memo : word\n<\/span><span style=\"font-weight: 400;\">end\u00a0 # =&gt; 'awesome'<\/span><\/pre><h4><b style=\"font-style: inherit;\">4. Advanced: Control Flow in Reduction<\/b><\/h4><p><span style=\"font-weight: 400;\">You can early-exit with <\/span><span style=\"font-weight: 400;\"><code>break<\/code><\/span><span style=\"font-weight: 400;\"> or handle conditions:<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">numbers = [1, 2, 3, 4, 5] <\/span><span style=\"font-weight: 400;\">result = numbers.reduce(0) do |sum, num|<\/span> <span style=\"font-weight: 400;\"> if num &gt; 3<\/span> <span style=\"font-weight: 400;\"> \u00a0\u00a0\u00a0 break sum\u00a0 # Stop early, return current sum<\/span> <span style=\"font-weight: 400;\"> fin<\/span> <span style=\"font-weight: 400;\"> \u00a0 sum + num<\/span> <span style=\"font-weight: 400;\">end\u00a0 # =&gt; 6 (1+2+3)<\/span><\/pre><p><span style=\"font-weight: 400;\">For skipping values, combine with <\/span><span style=\"font-weight: 400;\"><code>s\u00e9lectionner<\/code><\/span><span style=\"font-weight: 400;\"> or use <\/span><span style=\"font-weight: 400;\"><code>next<\/code><\/span><span style=\"font-weight: 400;\"> in the block (though <\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\"> doesn&#8217;t natively support <\/span><span style=\"font-weight: 400;\"><code>next<\/code><\/span><span style=\"font-weight: 400;\"> comme <\/span><span style=\"font-weight: 400;\"><code>chacun<\/code><\/span><span style=\"font-weight: 400;\">).<\/span><\/p><h3><b>Pitfalls and Gotchas<\/b><\/h3><h4><strong>1. No Initial Value: The Sneaky Default<\/strong><\/h4><p><span style=\"font-weight: 400;\">Without an initial value, <\/span><span style=\"font-weight: 400;\"><code>memo<\/code><\/span><span style=\"font-weight: 400;\"> starts as the <\/span><i><span style=\"font-weight: 400;\">first element<\/span><\/i><span style=\"font-weight: 400;\">, and the block runs one fewer time. This is fine for sums but disastrous for empty arrays:<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\">[].inject { |sum, n| sum + n }\u00a0 # =&gt; nil (no iteration!) <\/span><span style=\"font-weight: 400;\">[].inject(0) { |sum, n| sum + n }\u00a0 # =&gt; 0 (safe!)<\/span><\/pre><p><b>Pitfall Visualization<\/b><span style=\"font-weight: 400;\"> (empty array):<\/span><\/p><table><tbody><tr><td><b>Sc\u00e9nario<\/b><\/td><td><b>Initial?<\/b><\/td><td><b>Result<\/b><\/td><td><b>Why?<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">No initial<\/span><\/td><td><span style=\"font-weight: 400;\">N\/A<\/span><\/td><td><span style=\"font-weight: 400;\">n\u00e9ant<\/span><\/td><td><span style=\"font-weight: 400;\">Block never runs<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\">With 0<\/span><\/td><td><span style=\"font-weight: 400;\">Yes<\/span><\/td><td><span style=\"font-weight: 400;\">0<\/span><\/td><td><span style=\"font-weight: 400;\">Initial provides fallback<\/span><\/td><\/tr><\/tbody><\/table><p><span style=\"font-weight: 400;\">Always provide an initial value for robustness, especially with user-generated data.<\/span><\/p><h4><strong>2. Mutable vs. Immutable Accumulators<\/strong><\/h4><p><span style=\"font-weight: 400;\">If your block mutates <\/span><span style=\"font-weight: 400;\"><code>memo<\/code><\/span><span style=\"font-weight: 400;\"> but doesn&#8217;t return it, chaos ensues:<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis\n<\/code><\/span><span style=\"font-weight: 400;\"># WRONG: Mutates but returns wrong value <\/span><span style=\"font-weight: 400;\">h = [].inject({}) { |memo, _| memo[:count] = memo[:count] || 0; memo[:count] += 1;\u00a0 # Returns Integer! <\/span><span style=\"font-weight: 400;\"> memo[:count]\u00a0 # Oops, returns 1, not hash <\/span><span style=\"font-weight: 400;\">}\u00a0 # TypeError or wrong type!<\/span> <span style=\"font-weight: 400;\"># CORRECT: Return the accumulator <\/span><span style=\"font-weight: 400;\">h = [].inject({}) { |memo, _| memo[:count] = (memo[:count] || 0) + 1; memo }<\/span><\/pre><h4><b style=\"font-style: inherit;\">3. Performance on Large Collections<\/b><\/h4><p><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\"> is O(n), efficient for most cases, but nested structures can explode. For huge datasets, consider lazy evaluation with <\/span><span style=\"font-weight: 400;\"><code>lazy.reduce<\/code><\/span><span style=\"font-weight: 400;\">.<\/span><\/p><h3><strong><code>r\u00e9duire<\/code>\/<code>injecter<\/code> vs. <code>each_with_object<\/code>: When to Switch<\/strong><\/h3><p><span style=\"font-weight: 400;\"><code>each_with_object<\/code><\/span><span style=\"font-weight: 400;\"> is similar but doesn&#8217;t require returning the accumulator\u2014ideal for building immutable objects:<\/span><\/p><pre><span style=\"font-weight: 400;\"><code>rubis<\/code><\/span><br \/><br \/><span style=\"font-weight: 400;\"># With reduce: Must return memo explicitly<br \/><\/span><span style=\"font-weight: 400;\">hash = chars.reduce({}) { |h, c| h[c] = c.upcase; h }\u00a0 # Verbose return<\/span><br \/><br \/><span style=\"font-weight: 400;\"># With each_with_object: Implicit return<\/span><br \/><span style=\"font-weight: 400;\">hash = chars.each_with_object({}) { |c, h| h[c] = c.upcase }\u00a0 # Cleaner!<\/span><\/pre><p><span style=\"font-weight: 400;\">Utilisation <\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\"> when the block&#8217;s return value <\/span><i><span style=\"font-weight: 400;\">is<\/span><\/i><span style=\"font-weight: 400;\"> the next accumulator (e.g., sums). Use <\/span><span style=\"font-weight: 400;\"><code>each_with_object<\/code><\/span><span style=\"font-weight: 400;\"> for side-effect-heavy builds (e.g., appending to arrays\/hashes without reassigning).<\/span><\/p><table><tbody><tr><td><b>M\u00e9thode<\/b><\/td><td><b>Returns<\/b><\/td><td><b>Best For<\/b><\/td><td><b>Example Output Type<\/b><\/td><\/tr><tr><td><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><\/td><td><span style=\"font-weight: 400;\">Block return value<\/span><\/td><td><span style=\"font-weight: 400;\">Computations<\/span><\/td><td><span style=\"font-weight: 400;\">Number, String<\/span><\/td><\/tr><tr><td><span style=\"font-weight: 400;\"><code>each_with_object<\/code><\/span><\/td><td><span style=\"font-weight: 400;\">The object<\/span><\/td><td><span style=\"font-weight: 400;\">Mutations<\/span><\/td><td><span style=\"font-weight: 400;\">Hash, Array<\/span><\/td><\/tr><\/tbody><\/table><h3><b>Wrapping Up: Fold Your Collections Like a Pro<\/b><\/h3><p><span style=\"font-weight: 400;\"><code>injecter<\/code><\/span><span style=\"font-weight: 400;\"> et <\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\"> are Ruby&#8217;s Swiss Army knife for aggregation, transformation, and computation. Embrace their power, but always initialize safely and return intentionally. Next time you&#8217;re tempted by a <\/span><span style=\"font-weight: 400;\"><code>pour<\/code><\/span><span style=\"font-weight: 400;\"> loop with a growing variable, refactor to <\/span><span style=\"font-weight: 400;\"><code>r\u00e9duire<\/code><\/span><span style=\"font-weight: 400;\">\u2014your code will thank you.<\/span><\/p><p><span style=\"font-weight: 400;\">For teams building scalable, clean, and efficient <\/span><a href=\"https:\/\/www.railscarma.com\/fr\/developpement-dapplications-de-rails-personnalises\/\"><span style=\"font-weight: 400;\">Applications Ruby on Rails<\/span><\/a><span style=\"font-weight: 400;\">, partnering with <\/span><a href=\"https:\/\/www.railscarma.com\/fr\"><b>RailsCarma<\/b><\/a><span style=\"font-weight: 400;\"> brings the expertise to write elegant, maintainable code that performs at scale.<\/span><\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t  <div class=\"related-post slider\">\r\n        <div class=\"headline\">Articles Similaires<\/div>\r\n    <div class=\"post-list owl-carousel\">\r\n\r\n            <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\"Qu&#039;est-ce que Offliberty Ruby Gem et comment fonctionne-t-il ?\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/quest-ce-que-offliberty-ruby-gem-et-comment-fonctionne-t-il\/?related_post_from=41304\">\r\n\r\n      <img decoding=\"async\" width=\"800\" height=\"300\" src=\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/What-is-Offliberty-Ruby-Gem-and-How-It-Works.png\" class=\"attachment-full size-full wp-post-image\" alt=\"Offliberty Ruby Gem\" srcset=\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/What-is-Offliberty-Ruby-Gem-and-How-It-Works.png 800w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/What-is-Offliberty-Ruby-Gem-and-How-It-Works-300x113.png 300w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/What-is-Offliberty-Ruby-Gem-and-How-It-Works-768x288.png 768w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/What-is-Offliberty-Ruby-Gem-and-How-It-Works-18x7.png 18w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\"Qu&#039;est-ce que Offliberty Ruby Gem et comment fonctionne-t-il ?\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/quest-ce-que-offliberty-ruby-gem-et-comment-fonctionne-t-il\/?related_post_from=41304\">\r\n        Qu'est-ce que Offliberty Ruby Gem et comment fonctionne-t-il ?  <\/a>\r\n\r\n        <\/div>\r\n              <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\"M\u00e9thode Rails link_to : Le guide complet avec des exemples\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/rails-link_to-method-the-complete-guide-with-examples\/?related_post_from=41296\">\r\n\r\n      <img decoding=\"async\" width=\"800\" height=\"300\" src=\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Rails-link_to-Method-The-Complete-Guide-with-Examples.png\" class=\"attachment-full size-full wp-post-image\" alt=\"M\u00e9thode Rails link_to\" srcset=\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Rails-link_to-Method-The-Complete-Guide-with-Examples.png 800w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Rails-link_to-Method-The-Complete-Guide-with-Examples-300x113.png 300w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Rails-link_to-Method-The-Complete-Guide-with-Examples-768x288.png 768w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Rails-link_to-Method-The-Complete-Guide-with-Examples-18x7.png 18w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\"M\u00e9thode Rails link_to : Le guide complet avec des exemples\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/rails-link_to-method-the-complete-guide-with-examples\/?related_post_from=41296\">\r\n        M\u00e9thode Rails link_to : Le guide complet avec des exemples  <\/a>\r\n\r\n        <\/div>\r\n              <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\"Comment construire une plateforme SaaS \u00e9volutive en utilisant Ruby on Rails\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/how-to-build-a-scalable-saas-platform-using-ruby-on-rails\/?related_post_from=41273\">\r\n\r\n      <img decoding=\"async\" width=\"800\" height=\"300\" src=\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Build-a-SaaS-Platform-Using-Ruby-on-Rails.png\" class=\"attachment-full size-full wp-post-image\" alt=\"Construire une plateforme SaaS avec Ruby on Rails\" srcset=\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Build-a-SaaS-Platform-Using-Ruby-on-Rails.png 800w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Build-a-SaaS-Platform-Using-Ruby-on-Rails-300x113.png 300w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Build-a-SaaS-Platform-Using-Ruby-on-Rails-768x288.png 768w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Build-a-SaaS-Platform-Using-Ruby-on-Rails-18x7.png 18w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\"Comment construire une plateforme SaaS \u00e9volutive en utilisant Ruby on Rails\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/how-to-build-a-scalable-saas-platform-using-ruby-on-rails\/?related_post_from=41273\">\r\n        Comment construire une plateforme SaaS \u00e9volutive en utilisant Ruby on Rails  <\/a>\r\n\r\n        <\/div>\r\n              <div class=\"item\">\r\n            <div class=\"thumb post_thumb\">\r\n    <a  title=\"Guide de correspondance des expressions rationnelles en Ruby (2026) avec exemples\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/ruby-regex-match-guide-with-examples\/?related_post_from=41249\">\r\n\r\n      <img decoding=\"async\" width=\"800\" height=\"300\" src=\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Ruby-Regex-Match-Guide-with-Examples.png\" class=\"attachment-full size-full wp-post-image\" alt=\"Ruby Regex Match\" srcset=\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Ruby-Regex-Match-Guide-with-Examples.png 800w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Ruby-Regex-Match-Guide-with-Examples-300x113.png 300w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Ruby-Regex-Match-Guide-with-Examples-768x288.png 768w, https:\/\/www.railscarma.com\/wp-content\/uploads\/2026\/04\/Ruby-Regex-Match-Guide-with-Examples-18x7.png 18w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/>\r\n\r\n    <\/a>\r\n  <\/div>\r\n\r\n  <a class=\"title post_title\"  title=\"Guide de correspondance des expressions rationnelles en Ruby (2026) avec exemples\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/ruby-regex-match-guide-with-examples\/?related_post_from=41249\">\r\n        Guide de correspondance des expressions rationnelles en Ruby (2026) avec exemples  <\/a>\r\n\r\n        <\/div>\r\n      \r\n  <\/div>\r\n\r\n  <script>\r\n      <\/script>\r\n  <style>\r\n    .related-post {}\r\n\r\n    .related-post .post-list {\r\n      text-align: left;\r\n          }\r\n\r\n    .related-post .post-list .item {\r\n      margin: 10px;\r\n      padding: 10px;\r\n          }\r\n\r\n    .related-post .headline {\r\n      font-size: 14px !important;\r\n      color: #999999 !important;\r\n          }\r\n\r\n    .related-post .post-list .item .post_thumb {\r\n      max-height: 220px;\r\n      margin: 10px 0px;\r\n      padding: 0px;\r\n      display: block;\r\n          }\r\n\r\n    .related-post .post-list .item .post_title {\r\n      font-size: 14px;\r\n      color: #000000;\r\n      margin: 10px 0px;\r\n      padding: 0px;\r\n      display: block;\r\n      text-decoration: none;\r\n          }\r\n\r\n    .related-post .post-list .item .post_excerpt {\r\n      font-size: 12px;\r\n      color: #3f3f3f;\r\n      margin: 10px 0px;\r\n      padding: 0px;\r\n      display: block;\r\n      text-decoration: none;\r\n          }\r\n\r\n    .related-post .owl-dots .owl-dot {\r\n          }\r\n\r\n      <\/style>\r\n      <script>\r\n      jQuery(document).ready(function($) {\r\n        $(\".related-post .post-list\").owlCarousel({\r\n          items: 2,\r\n          responsiveClass: true,\r\n          responsive: {\r\n            0: {\r\n              items: 1,\r\n            },\r\n            768: {\r\n              items: 2,\r\n            },\r\n            1200: {\r\n              items: 2,\r\n            }\r\n          },\r\n                      rewind: true,\r\n                                loop: true,\r\n                                center: false,\r\n                                autoplay: true,\r\n            autoplayHoverPause: true,\r\n                                nav: true,\r\n            navSpeed: 1000,\r\n            navText: ['<i class=\"fas fa-chevron-left\"><\/i>', '<i class=\"fas fa-chevron-right\"><\/i>'],\r\n                                dots: false,\r\n            dotsSpeed: 1200,\r\n                                                    rtl: false,\r\n          \r\n        });\r\n      });\r\n    <\/script>\r\n  <\/div>","protected":false},"excerpt":{"rendered":"<p>Ruby&#8217;s Enumerable module provides a suite of methods for working with collections like arrays, hashes, and ranges. Among the most versatile are inject and reduce. If you&#8217;re coming from languages like JavaScript or Python, these might remind you of the reduce() function\u2014 and for good reason. In Ruby, inject and reduce are exact aliases for &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/ruby-regex-match-guide-with-examples\/\"> <span class=\"screen-reader-text\">Guide de correspondance des expressions rationnelles en Ruby (2026) avec exemples<\/span> Lire la suite \u00bb<\/a><\/p>","protected":false},"author":11,"featured_media":40552,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1224],"tags":[],"class_list":["post-40509","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration<\/title>\n<meta name=\"description\" content=\"Ruby Inject and Ruby Reduce are powerful enumeration aliases enabling clean aggregation transformation and concise code logic.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.railscarma.com\/fr\/blog\/ruby-inject-and-ruby-reduce\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration\" \/>\n<meta property=\"og:description\" content=\"Ruby Inject and Ruby Reduce are powerful enumeration aliases enabling clean aggregation transformation and concise code logic.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.railscarma.com\/fr\/blog\/ruby-inject-and-ruby-reduce\/\" \/>\n<meta property=\"og:site_name\" content=\"RailsCarma - Ruby on Rails Development Company specializing in Offshore Development\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/RailsCarma\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-12-05T08:07:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-12-05T08:07:56+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"ashish\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@railscarma\" \/>\n<meta name=\"twitter:site\" content=\"@railscarma\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"ashish\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/\"},\"author\":{\"name\":\"ashish\",\"@id\":\"https:\/\/www.railscarma.com\/#\/schema\/person\/9699b14852b308edfeb03096b33c7a7a\"},\"headline\":\"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration\",\"datePublished\":\"2025-12-05T08:07:53+00:00\",\"dateModified\":\"2025-12-05T08:07:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/\"},\"wordCount\":801,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.railscarma.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png\",\"articleSection\":[\"Blogs\"],\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/\",\"url\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/\",\"name\":\"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration\",\"isPartOf\":{\"@id\":\"https:\/\/www.railscarma.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png\",\"datePublished\":\"2025-12-05T08:07:53+00:00\",\"dateModified\":\"2025-12-05T08:07:56+00:00\",\"description\":\"Ruby Inject and Ruby Reduce are powerful enumeration aliases enabling clean aggregation transformation and concise code logic.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#primaryimage\",\"url\":\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png\",\"contentUrl\":\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png\",\"width\":800,\"height\":300,\"caption\":\"Ruby Inject and Ruby Reduce\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.railscarma.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.railscarma.com\/#website\",\"url\":\"https:\/\/www.railscarma.com\/\",\"name\":\"RailsCarma - Ruby on Rails Development Company specializing in Offshore Development\",\"description\":\"RailsCarma is a Ruby on Rails Development Company in Bangalore. We specialize in Offshore Ruby on Rails Development based out in USA and India. Hire experienced Ruby on Rails developers for the ultimate Web Experience.\",\"publisher\":{\"@id\":\"https:\/\/www.railscarma.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.railscarma.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.railscarma.com\/#organization\",\"name\":\"RailsCarma\",\"url\":\"https:\/\/www.railscarma.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/www.railscarma.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2020\/08\/railscarma_logo.png\",\"contentUrl\":\"https:\/\/www.railscarma.com\/wp-content\/uploads\/2020\/08\/railscarma_logo.png\",\"width\":200,\"height\":46,\"caption\":\"RailsCarma\"},\"image\":{\"@id\":\"https:\/\/www.railscarma.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/RailsCarma\/\",\"https:\/\/x.com\/railscarma\",\"https:\/\/www.linkedin.com\/company\/railscarma\/\",\"https:\/\/myspace.com\/railscarma\",\"https:\/\/in.pinterest.com\/railscarma\/\",\"https:\/\/www.youtube.com\/channel\/UCx3Wil-aAnDARuatTEyMdpg\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.railscarma.com\/#\/schema\/person\/9699b14852b308edfeb03096b33c7a7a\",\"name\":\"ashish\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/www.railscarma.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/204411c7d72714bc32d5ac6398e0596896318386bd537860fdd14ce905a79e07?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/204411c7d72714bc32d5ac6398e0596896318386bd537860fdd14ce905a79e07?s=96&d=mm&r=g\",\"caption\":\"ashish\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration","description":"Ruby Inject and Ruby Reduce are powerful enumeration aliases enabling clean aggregation transformation and concise code logic.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.railscarma.com\/fr\/blog\/ruby-inject-and-ruby-reduce\/","og_locale":"fr_FR","og_type":"article","og_title":"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration","og_description":"Ruby Inject and Ruby Reduce are powerful enumeration aliases enabling clean aggregation transformation and concise code logic.","og_url":"https:\/\/www.railscarma.com\/fr\/blog\/ruby-inject-and-ruby-reduce\/","og_site_name":"RailsCarma - Ruby on Rails Development Company specializing in Offshore Development","article_publisher":"https:\/\/www.facebook.com\/RailsCarma\/","article_published_time":"2025-12-05T08:07:53+00:00","article_modified_time":"2025-12-05T08:07:56+00:00","og_image":[{"width":800,"height":300,"url":"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png","type":"image\/png"}],"author":"ashish","twitter_card":"summary_large_image","twitter_creator":"@railscarma","twitter_site":"@railscarma","twitter_misc":{"\u00c9crit par":"ashish","Dur\u00e9e de lecture estim\u00e9e":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#article","isPartOf":{"@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/"},"author":{"name":"ashish","@id":"https:\/\/www.railscarma.com\/#\/schema\/person\/9699b14852b308edfeb03096b33c7a7a"},"headline":"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration","datePublished":"2025-12-05T08:07:53+00:00","dateModified":"2025-12-05T08:07:56+00:00","mainEntityOfPage":{"@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/"},"wordCount":801,"commentCount":0,"publisher":{"@id":"https:\/\/www.railscarma.com\/#organization"},"image":{"@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#primaryimage"},"thumbnailUrl":"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png","articleSection":["Blogs"],"inLanguage":"fr-FR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/","url":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/","name":"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration","isPartOf":{"@id":"https:\/\/www.railscarma.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#primaryimage"},"image":{"@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#primaryimage"},"thumbnailUrl":"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png","datePublished":"2025-12-05T08:07:53+00:00","dateModified":"2025-12-05T08:07:56+00:00","description":"Ruby Inject and Ruby Reduce are powerful enumeration aliases enabling clean aggregation transformation and concise code logic.","breadcrumb":{"@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/"]}]},{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#primaryimage","url":"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png","contentUrl":"https:\/\/www.railscarma.com\/wp-content\/uploads\/2025\/12\/Ruby-Inject-and-Ruby-Reduce-Aliases-for-Powerful-Enumeration.png","width":800,"height":300,"caption":"Ruby Inject and Ruby Reduce"},{"@type":"BreadcrumbList","@id":"https:\/\/www.railscarma.com\/blog\/ruby-inject-and-ruby-reduce\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.railscarma.com\/"},{"@type":"ListItem","position":2,"name":"Ruby Inject and Ruby Reduce: Aliases for Powerful Enumeration"}]},{"@type":"WebSite","@id":"https:\/\/www.railscarma.com\/#website","url":"https:\/\/www.railscarma.com\/","name":"RailsCarma - Soci\u00e9t\u00e9 de d\u00e9veloppement Ruby on Rails sp\u00e9cialis\u00e9e dans le d\u00e9veloppement offshore","description":"RailsCarma est une soci\u00e9t\u00e9 de d\u00e9veloppement Ruby on Rails \u00e0 Bangalore. Nous sommes sp\u00e9cialis\u00e9s dans le d\u00e9veloppement offshore Ruby on Rails, bas\u00e9s aux \u00c9tats-Unis et en Inde. Embauchez des d\u00e9veloppeurs Ruby on Rails exp\u00e9riment\u00e9s pour une exp\u00e9rience Web ultime.","publisher":{"@id":"https:\/\/www.railscarma.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.railscarma.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":"Organization","@id":"https:\/\/www.railscarma.com\/#organization","name":"RailsCarma","url":"https:\/\/www.railscarma.com\/","logo":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/www.railscarma.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.railscarma.com\/wp-content\/uploads\/2020\/08\/railscarma_logo.png","contentUrl":"https:\/\/www.railscarma.com\/wp-content\/uploads\/2020\/08\/railscarma_logo.png","width":200,"height":46,"caption":"RailsCarma"},"image":{"@id":"https:\/\/www.railscarma.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/RailsCarma\/","https:\/\/x.com\/railscarma","https:\/\/www.linkedin.com\/company\/railscarma\/","https:\/\/myspace.com\/railscarma","https:\/\/in.pinterest.com\/railscarma\/","https:\/\/www.youtube.com\/channel\/UCx3Wil-aAnDARuatTEyMdpg"]},{"@type":"Person","@id":"https:\/\/www.railscarma.com\/#\/schema\/person\/9699b14852b308edfeb03096b33c7a7a","name":"ashish","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/www.railscarma.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/204411c7d72714bc32d5ac6398e0596896318386bd537860fdd14ce905a79e07?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/204411c7d72714bc32d5ac6398e0596896318386bd537860fdd14ce905a79e07?s=96&d=mm&r=g","caption":"ashish"}}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/posts\/40509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/comments?post=40509"}],"version-history":[{"count":0,"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/posts\/40509\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/media\/40552"}],"wp:attachment":[{"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/media?parent=40509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/categories?post=40509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.railscarma.com\/fr\/wp-json\/wp\/v2\/tags?post=40509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}