{"id":3433,"date":"2022-07-12T12:13:20","date_gmt":"2022-07-12T12:13:20","guid":{"rendered":"http:\/\/the-codest.localhost\/blog\/hash-to-use-or-not-to-use\/"},"modified":"2026-03-11T05:57:22","modified_gmt":"2026-03-11T05:57:22","slug":"hash-at-bruge-eller-ikke-at-bruge","status":"publish","type":"post","link":"https:\/\/thecodest.co\/da\/blog\/hash-to-use-or-not-to-use\/","title":{"rendered":"Hash: At bruge eller ikke at bruge"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>When we want to aggregate some stuff, very often we use <code>#each_with_object<\/code> or extend the regular <strong>loop<\/strong> using <code>#with_object<\/code>. But in most cases <strong> <a href=\"https:\/\/thecodest.co\/da\/case-studies\/providing-a-team-of-ruby-developers-for-a-fintech-company\/\">Ruby<\/a> developers<\/strong> are using a plain hash as the aggregator and maybe it&#8217;s fine, but in this article, I&#8217;d like to show you that it doesn&#8217;t always have to be a hash.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Case<\/h2>\n\n\n\n<p>We assume that all the files are placed in one directory (<code>people<\/code>).<\/p>\n\n\n\n<p>Let&#8217;s say we have the following <code>people\/people.csv<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"><code> First Name,Last Name,Age\n John,Doe,24\n Jane,Dee,45\n Josh,Bee,55\n Andrea,Boya,34\n Andrew,Moore,54<\/code><\/code><\/pre>\n\n\n\n<p>We want to find the total of rows and the average age &#8211; we could write the following script:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">people\/parser.rb\n\nrequire 'csv'\n\naggregated = CSV.foreach('people.csv', headers: true)\n.withobject({ total: 0, totalage: 0 }) do |row, agg|\nagg[:total] += 1\nagg[:totalage] += row['Age'].toi\nend\n\ntotal = aggregated[:total]\naverageage = aggregated[:totalage].to_f \/ total\n\nputs \"Total: #{total}\"\nputs \"Average age: #{average_age}\"<\/code><\/pre>\n\n\n\n<p>And yes, it does the thing but reading such a <a href=\"https:\/\/thecodest.co\/da\/dictionary\/what-is-code-refactoring\/\">code<\/a> is a doubtful pleasure. It feels like a too low level. We can improve it by providing a dedicated aggregator for the <strong>loop<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">people\/age_aggregator.rb\n\nclass AgeAggregator\nattrreader :total, :totalage\n\ndef initialize\n@total = 0\n@total_age = 0\nend\n\ndef increment!\n@total += 1\nend\n\ndef incrementage!(age)\n@totalage += age\nend\n\ndef averageage\ntotalage.to_f \/ total\nend\nend<\/code><\/pre>\n\n\n\n<p>And then our loop would look as below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">people\/parser.rb\n\nrequire 'csv'\nrequirerelative '.\/ageaggregator.rb'\n\naggregated = CSV.foreach('people.csv', headers: true)\n.withobject(AgeAggregator.new) do |row, agg|\nagg.increment!\nagg.incrementage!(row['Age'].to_i)\nend\n\nputs \"Total: #{aggregated.total}\"\nputs \"Average age: #{aggregated.average_age}\"<\/code><\/pre>\n\n\n\n<p>I think it&#8217;s much clearer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>We&#8217;ve written more code, but our lower-level details are extracted to the separate class. Now the main script reads much better.<\/p>\n\n\n\n<p>Of course, you can argue that the example is too simple to put so much effort into refactoring, but c&#8217;mon &#8211; it&#8217;s just an example ;). If you had to aggregate more <a href=\"https:\/\/thecodest.co\/da\/blog\/app-data-collection-security-risks-value-and-types-explored\/\">data<\/a>, such aggregator objects are really the way to rescue.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/thecodest.co\/contact\"><img decoding=\"async\" src=\"\/app\/uploads\/2024\/05\/interested_in_cooperation_.png\" alt=\"cooperation banner\"\/><\/a><\/figure>\n\n\n\n<p><strong>Read more:<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/thecodest.co\/blog\/pros-and-cons-of-ruby-software-development\/\">Pros and cons of Ruby software development<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/thecodest.co\/blog\/rails-and-other-means-of-transport\">Rails and Other Means of Transport<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/thecodest.co\/blog\/rails-development-with-tmux-vim-fzf-ripgrep\">Rails Development with TMUX, Vim, Fzf + Ripgrep<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>L\u00e6s en artikel fra vores Ruby-ekspert og find ud af, hvorfor du ikke altid beh\u00f8ver at bruge hash.<\/p>","protected":false},"author":2,"featured_media":3434,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-3433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Hash: To Use or Not to Use - The Codest<\/title>\n<meta name=\"description\" content=\"Read an article coming from our Ruby Expert and learn why you don&#039;t need to always youse hash.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/thecodest.co\/da\/blog\/hash-at-bruge-eller-ikke-at-bruge\/\" \/>\n<meta property=\"og:locale\" content=\"da_DK\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hash: To Use or Not to Use - The Codest\" \/>\n<meta property=\"og:description\" content=\"Read an article coming from our Ruby Expert and learn why you don&#039;t need to always youse hash.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/thecodest.co\/da\/blog\/hash-at-bruge-eller-ikke-at-bruge\/\" \/>\n<meta property=\"og:site_name\" content=\"The Codest\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-12T12:13:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-11T05:57:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png\" \/>\n\t<meta property=\"og:image:width\" content=\"960\" \/>\n\t<meta property=\"og:image:height\" content=\"540\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"thecodest\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"thecodest\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/\"},\"author\":{\"name\":\"thecodest\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\"},\"headline\":\"Hash: To Use or Not to Use\",\"datePublished\":\"2022-07-12T12:13:20+00:00\",\"dateModified\":\"2026-03-11T05:57:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/\"},\"wordCount\":239,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png\",\"articleSection\":[\"Software Development\"],\"inLanguage\":\"da-DK\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/\",\"url\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/\",\"name\":\"Hash: To Use or Not to Use - The Codest\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png\",\"datePublished\":\"2022-07-12T12:13:20+00:00\",\"dateModified\":\"2026-03-11T05:57:22+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\"},\"description\":\"Read an article coming from our Ruby Expert and learn why you don't need to always youse hash.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/#breadcrumb\"},\"inLanguage\":\"da-DK\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"da-DK\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/#primaryimage\",\"url\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png\",\"contentUrl\":\"https:\\\/\\\/thecodest.co\\\/app\\\/uploads\\\/2024\\\/05\\\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png\",\"width\":960,\"height\":540},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/blog\\\/hash-to-use-or-not-to-use\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/thecodest.co\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hash: To Use or Not to Use\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#website\",\"url\":\"https:\\\/\\\/thecodest.co\\\/\",\"name\":\"The Codest\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/thecodest.co\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"da-DK\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/thecodest.co\\\/#\\\/schema\\\/person\\\/7e3fe41dfa4f4e41a7baad4c6e0d4f76\",\"name\":\"thecodest\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"da-DK\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g\",\"caption\":\"thecodest\"},\"url\":\"https:\\\/\\\/thecodest.co\\\/da\\\/author\\\/thecodest\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hash: At bruge eller ikke at bruge - The Codest","description":"L\u00e6s en artikel fra vores Ruby-ekspert og find ud af, hvorfor du ikke altid beh\u00f8ver at bruge hash.","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:\/\/thecodest.co\/da\/blog\/hash-at-bruge-eller-ikke-at-bruge\/","og_locale":"da_DK","og_type":"article","og_title":"Hash: To Use or Not to Use - The Codest","og_description":"Read an article coming from our Ruby Expert and learn why you don't need to always youse hash.","og_url":"https:\/\/thecodest.co\/da\/blog\/hash-at-bruge-eller-ikke-at-bruge\/","og_site_name":"The Codest","article_published_time":"2022-07-12T12:13:20+00:00","article_modified_time":"2026-03-11T05:57:22+00:00","og_image":[{"width":960,"height":540,"url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png","type":"image\/png"}],"author":"thecodest","twitter_card":"summary_large_image","twitter_misc":{"Written by":"thecodest","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/#article","isPartOf":{"@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/"},"author":{"name":"thecodest","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76"},"headline":"Hash: To Use or Not to Use","datePublished":"2022-07-12T12:13:20+00:00","dateModified":"2026-03-11T05:57:22+00:00","mainEntityOfPage":{"@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/"},"wordCount":239,"commentCount":0,"image":{"@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png","articleSection":["Software Development"],"inLanguage":"da-DK","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/","url":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/","name":"Hash: At bruge eller ikke at bruge - The Codest","isPartOf":{"@id":"https:\/\/thecodest.co\/#website"},"primaryImageOfPage":{"@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/#primaryimage"},"image":{"@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/#primaryimage"},"thumbnailUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png","datePublished":"2022-07-12T12:13:20+00:00","dateModified":"2026-03-11T05:57:22+00:00","author":{"@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76"},"description":"L\u00e6s en artikel fra vores Ruby-ekspert og find ud af, hvorfor du ikke altid beh\u00f8ver at bruge hash.","breadcrumb":{"@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/#breadcrumb"},"inLanguage":"da-DK","potentialAction":[{"@type":"ReadAction","target":["https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/"]}]},{"@type":"ImageObject","inLanguage":"da-DK","@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/#primaryimage","url":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png","contentUrl":"https:\/\/thecodest.co\/app\/uploads\/2024\/05\/loop_with_an_object_-_it_doesn_t_always_have_to_be_a_hash-.png","width":960,"height":540},{"@type":"BreadcrumbList","@id":"https:\/\/thecodest.co\/blog\/hash-to-use-or-not-to-use\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/thecodest.co\/"},{"@type":"ListItem","position":2,"name":"Hash: To Use or Not to Use"}]},{"@type":"WebSite","@id":"https:\/\/thecodest.co\/#website","url":"https:\/\/thecodest.co\/","name":"Codest","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/thecodest.co\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"da-DK"},{"@type":"Person","@id":"https:\/\/thecodest.co\/#\/schema\/person\/7e3fe41dfa4f4e41a7baad4c6e0d4f76","name":"thecodest","image":{"@type":"ImageObject","inLanguage":"da-DK","@id":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5dbfe6a1e8c86e432e8812759e34e6fe82ebac75119ae3237a6c1311fa19caf4?s=96&d=mm&r=g","caption":"thecodest"},"url":"https:\/\/thecodest.co\/da\/author\/thecodest\/"}]}},"_links":{"self":[{"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/posts\/3433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/comments?post=3433"}],"version-history":[{"count":6,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/posts\/3433\/revisions"}],"predecessor-version":[{"id":8349,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/posts\/3433\/revisions\/8349"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/media\/3434"}],"wp:attachment":[{"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/media?parent=3433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/categories?post=3433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thecodest.co\/da\/wp-json\/wp\/v2\/tags?post=3433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}