{"id":47445,"date":"2013-07-13T17:26:42","date_gmt":"2013-07-13T14:26:42","guid":{"rendered":"https:\/\/www.altoros.com\/blog\/?p=47445"},"modified":"2022-01-03T15:27:53","modified_gmt":"2022-01-03T12:27:53","slug":"two-or-three-powerful-iterators","status":"publish","type":"post","link":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/","title":{"rendered":"Two Powerful Iterator Methods"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#Simplifying_development_routines\" >Simplifying development routines<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#The_checkitem_method\" >The check(item) method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#The_json_items_method\" >The json_items method<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#Further_reading\" >Further reading<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#About_the_author\" >About the author<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Simplifying_development_routines\"><\/span>Simplifying development routines<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2013\/07\/ruby_iterators_logo-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2013\/07\/ruby_iterators_logo-1.jpg\" alt=\"\" width=\"158\" height=\"85\" class=\"alignright size-full wp-image-57326\" \/><\/a><\/p>\n<p>Everybody who deals with Ruby knows that collections can be tedious. However, thanks to Ruby, we have a large arsenal to deal with them! This post overviews two methods that can greatly simplify your development routines. You will get real-life examples that demonstrate how to optimize the code, as well as some tips to increase code readability and enhance system support in the future.<\/p>\n<p>Recently, I&#8217;ve written a small JSON parser of the Flickr public feed, so I\u2019ll use some methods of that module as examples. Basically, the module had to fetch a JSON object returned by the Flickr API and parse it to a Ruby JSON object checking some validations. This kind of functionality doesn\u2019t take more than a dozen of Ruby lines, but it\u2019s a good example to achieve our goal.<\/p>\n<p>We\u2019ll focus on two methods of the module:<\/p>\n<ul>\n<li><code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">json_items<\/code> returns an array of hashes. Each hash represents a feed item from the Flickr API.<\/li>\n<li><code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">check(item)<\/code> checks if the given item hash satisfies some validations.<\/li>\n<\/ul>\n<p>We\u2019ll also skip all the URL encoding and some validation stuff to focus on the iterative methods.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"The_checkitem_method\"><\/span>The <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">check(item)<\/code> method<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>First, lets take a look at the <b>check<\/b> method. The goal of this method is to loop inside a item (a hash) and update it on the fly. A valid approach could look as shown below.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">def check(item)\r\n  new_item = {}\r\n  item.each do |key, value|\r\n    new_item&#x5B;key] = value.empty? ? &quot;No data&quot; : value\r\n  end\r\n  new_item\r\nend\r\n<\/pre>\n<p>This code certainly works, but it also has some design issues like a temp (and unnecessary) hash.<\/p>\n<p>As Ruby developers, we should take care of the Ruby API in such cases. The code bellow can be good as a start point, but it does not take long to realize that it &#8220;smells.&#8221; So if we look at the hash doc we can find an alternative and a prettier solution with the <a href=\"https:\/\/ruby-doc.org\/core-2.0.0\/Hash.html#method-i-update\">update<\/a> method:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">def check(item)\r\n  item.update(item) do |key, value|\r\n    value.empty? ? &quot;No data&quot; : value\r\n  end\r\nend\r\n<\/pre>\n<p>Here, we\u2019re using the update method of hash that is employed when you want to update the content of a hash based on some other one. In this case, we only have a single hash, so we apply the update method to itself. By using the update method, we prevented the creation of an unnecessary hash, and we got our code more readable and clearer.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"The_json_items_method\"><\/span>The <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">json_items<\/code> method<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Now it\u2019s time to explore the <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">json_items<\/code> method. As previously stated, this method should return an array of hashes that must be validated with our check method. A possible code could be as illustrated below.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">def json_items\r\n  new_items = &#x5B;]\r\n  json = get_some_json_data\r\n  json.each do |item|\r\n    new_items &amp;lt;&amp;lt; check(item)\r\n  end\r\n  new_items\r\nend\r\n<\/pre>\n<p>I really hate this repetitive code snippet, it\u2019s very common when you want to create an array based on some other one.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">def method_name\r\n  temp_array = &#x5B;]\r\n  original_array.each do |item|\r\n    temp_array &amp;lt;&amp;lt; some_stuff_with(item)\r\n  end\r\n  temp_array\r\nend\r\n<\/pre>\n<p>Thanks to Ruby, we have an awesome method for array objects when we have to deal with this kind of snippets: <a href=\"https:\/\/apidock.com\/ruby\/Enumerable\/inject\">inject<\/a>. This awesome method allows us to make magical things. For instance, if we want to know the average word length of a document or a string, we can do as follows:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">def average_word_length\r\n  total = 0.0\r\n  words.each{ |word| total += word.size }\r\n  total \/ word_count\r\nend\r\n<\/pre>\n<p>This can be done more concisely with inject.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">def average_word_length\r\n  total = words.inject(0.0){ |result, word| word.size + result}\r\n  total \/ word_count\r\nend\r\n<\/pre>\n<p>As its name suggests, inject \u201cinjects\u201d an initial object (0.0 in the example above) and uses it as the initial value of the \u201cmemo\u201d (result in the code), then iterates the given block like each of the methods does. Inject is very flexible, if you do not explicitly specify an initial value in the inject, then the first element of collection is used as the initial value.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">def sum\r\n  (1..10).inject{ |sum, n| sum + n }  #returns 55 (= 1+2+...+10)\r\nend\r\n<\/pre>\n<p>You can also use inject with hashes. When running inject on a hash, the latter is first converted to an array before being passed through. By applying it to the <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">json_items method<\/code>, we get:<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">def json_items\r\n  json = JSON.parse(get_json)&#x5B;&quot;items&quot;]\r\n  items = json.inject(&#x5B;]) do |items, item|\r\n    items &amp;lt;&amp;lt; check(item)\r\n  end\r\nend\r\n<\/pre>\n<p>So, with inject, we no longer need to instantiate a temp var, which allows us to build a new array on the fly and have a more concise and readable code.<\/p>\n<p>Inject inherently projects a set of collection values to an unique value. In other words, it resembles a many-to-one function. For this reason, inject has a well-known alias\u2014reduce. In maths and other programming languages, it also has other names like fold, accumulate, aggregate, and compress. This kind of functions analyzes a recursive data structure and recombines through use of a given combining operation the results of recursively processing its constituent parts, building up a return value.<\/p>\n<p>Therefore, the <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">json_items example<\/code> is not the best example to use inject, because we\u2019re trying to achieve a one-to-one conversion. In such cases, we should use other methods, such as map or collect, that fit better with what we\u2019re trying to do.<\/p>\n<pre class=\"brush: ruby; title: ; notranslate\" title=\"\">def json_items\r\n  json = JSON.parse(get_json)&#x5B;&quot;items&quot;]\r\n  json.map{ |item| check(item) }\r\nend\r\n<\/pre>\n<p>For more details on hash update, please check out <a href=\"https:\/\/apidock.com\/ruby\/Hash\/update\">this documentation<\/a> and <a href=\"https:\/\/ruby-doc.org\/core-2.0.0\/Hash.html#method-i-update\">this overview<\/a>. More isights into enumerable inject are available in <a href=\"https:\/\/apidock.com\/ruby\/Enumerable\/inject\">these docs<\/a>, as well. <\/p>\n<p>Ruby provides us awesome methods, we should use them wisely and follow the Ruby philosophy. So, I encourage you to employ these methods to make clearer and beautiful Ruby apps!<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Further_reading\"><\/span>Further reading<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><a href=\"https:\/\/www.altoros.com\/blog\/running-capybara-tests-for-ruby-applications-in-remote-browsers\/\">How to Run Capybara Tests for Ruby Applications in Remote Browsers<\/a><\/li>\n<li><a href=\"https:\/\/www.altoros.com\/blog\/implementing-and-improving-postgresql-fulltext-search\/\">How to Add PostgreSQL Full-Text Search to Ruby Apps and Optimize Its Performance<\/a><\/li>\n<li><a href=\"https:\/\/www.altoros.com\/blog\/lets-test-it-well-and-simply-and-smartly-2\/\">Let\u2019s Test It Well: Simply and Smartly<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"About_the_author\"><\/span>About the author<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2013\/07\/emiliano-coppo.png\" width=\"110\" class=\"alignright size-thumbnail wp-image-64162\" \/><\/p>\n<p><small><a href=\"https:\/\/www.linkedin.com\/in\/ecoppo\/\" rel=\"noopener noreferrer\" target=\"_blank\">Emiliano Coppo<\/a> is interested in developing Ruby and Ruby On Rails Applications. He is proficient in JavaScript, HTML, CSS, jQuery, jQuery UI and Mobile, PostgreSQL, MySQL. Emiliano describes himself as a passionate coder, always trying to learn new technologies and looking for challenging projects. Find him on <a href=\"https:\/\/github.com\/bismark64\" rel=\"noopener noreferrer\" target=\"_blank\">GitHub<\/a>.<\/small><\/p>\n<hr \/>\n<p><center><small>This blog post was written by <a href=\"https:\/\/github.com\/bismark64\" rel=\"noopener noreferrer\" target=\"_blank\">Emiliano Coppo<\/a> with suggestions and tips from<br \/>\n<a href=\"https:\/\/github.com\/avsej\" rel=\"noopener noreferrer\" target=\"_blank\">Sergey Avseyev<\/a> and <a href=\"https:\/\/github.com\/wacko\" rel=\"noopener noreferrer\" target=\"_blank\">Joaqu\u00edn Vicente<\/a>.<\/small><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Two Ruby iterator methods I really love, thanks to its simplicity and beauty<\/p>\n","protected":false},"author":34,"featured_media":58501,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[214],"tags":[1000,895],"class_list":["post-47445","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-github","tag-research-and-development"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Two Powerful Iterator Methods | Altoros<\/title>\n<meta name=\"description\" content=\"Learn how check(item) and check(item) methods help to optimize Ruby code, increase its readability, and simplify support.\" \/>\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.altoros.com\/blog\/two-or-three-powerful-iterators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Two Powerful Iterator Methods | Altoros\" \/>\n<meta property=\"og:description\" content=\"Two Ruby iterator methods I really love, thanks to its simplicity and beauty\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/\" \/>\n<meta property=\"og:site_name\" content=\"Altoros\" \/>\n<meta property=\"article:published_time\" content=\"2013-07-13T14:26:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-03T12:27:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2013\/07\/two-powerful-iterator-methods-featured-image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"576\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Alena Vasilenko\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alena Vasilenko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/\"},\"author\":{\"name\":\"Alena Vasilenko\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#\\\/schema\\\/person\\\/019e8147b835bc8f1b4abd8a4fa42c7f\"},\"headline\":\"Two Powerful Iterator Methods\",\"datePublished\":\"2013-07-13T14:26:42+00:00\",\"dateModified\":\"2022-01-03T12:27:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/\"},\"wordCount\":1116,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/07\\\/two-powerful-iterator-methods-featured-image.png\",\"keywords\":[\"GitHub\",\"Research and Development\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/\",\"name\":\"Two Powerful Iterator Methods | Altoros\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/07\\\/two-powerful-iterator-methods-featured-image.png\",\"datePublished\":\"2013-07-13T14:26:42+00:00\",\"dateModified\":\"2022-01-03T12:27:53+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#\\\/schema\\\/person\\\/019e8147b835bc8f1b4abd8a4fa42c7f\"},\"description\":\"Two Ruby iterator methods I really love, thanks to its simplicity and beauty\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/07\\\/two-powerful-iterator-methods-featured-image.png\",\"contentUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2013\\\/07\\\/two-powerful-iterator-methods-featured-image.png\",\"width\":1024,\"height\":576},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/two-or-three-powerful-iterators\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Two Powerful Iterator Methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/\",\"name\":\"Altoros\",\"description\":\"Insight\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#\\\/schema\\\/person\\\/019e8147b835bc8f1b4abd8a4fa42c7f\",\"name\":\"Alena Vasilenko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/alena-vasilenko-author-e1561752194994-96x96.jpg\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/alena-vasilenko-author-e1561752194994-96x96.jpg\",\"contentUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/alena-vasilenko-author-e1561752194994-96x96.jpg\",\"caption\":\"Alena Vasilenko\"},\"description\":\"Alena Vasilenko is Communications Manager at Altoros. She has proven track record of supporting R&amp;D engineers in their research activities on such topics as big data and cloud computing, translating the research results into easy-to-understand stories.\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/author\\\/alena-vasilenko\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Two Powerful Iterator Methods | Altoros","description":"Learn how check(item) and check(item) methods help to optimize Ruby code, increase its readability, and simplify support.","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.altoros.com\/blog\/two-or-three-powerful-iterators\/","og_locale":"en_US","og_type":"article","og_title":"Two Powerful Iterator Methods | Altoros","og_description":"Two Ruby iterator methods I really love, thanks to its simplicity and beauty","og_url":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/","og_site_name":"Altoros","article_published_time":"2013-07-13T14:26:42+00:00","article_modified_time":"2022-01-03T12:27:53+00:00","og_image":[{"width":1024,"height":576,"url":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2013\/07\/two-powerful-iterator-methods-featured-image.png","type":"image\/png"}],"author":"Alena Vasilenko","twitter_misc":{"Written by":"Alena Vasilenko","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#article","isPartOf":{"@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/"},"author":{"name":"Alena Vasilenko","@id":"https:\/\/www.altoros.com\/blog\/#\/schema\/person\/019e8147b835bc8f1b4abd8a4fa42c7f"},"headline":"Two Powerful Iterator Methods","datePublished":"2013-07-13T14:26:42+00:00","dateModified":"2022-01-03T12:27:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/"},"wordCount":1116,"commentCount":0,"image":{"@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#primaryimage"},"thumbnailUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2013\/07\/two-powerful-iterator-methods-featured-image.png","keywords":["GitHub","Research and Development"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/","url":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/","name":"Two Powerful Iterator Methods | Altoros","isPartOf":{"@id":"https:\/\/www.altoros.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#primaryimage"},"image":{"@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#primaryimage"},"thumbnailUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2013\/07\/two-powerful-iterator-methods-featured-image.png","datePublished":"2013-07-13T14:26:42+00:00","dateModified":"2022-01-03T12:27:53+00:00","author":{"@id":"https:\/\/www.altoros.com\/blog\/#\/schema\/person\/019e8147b835bc8f1b4abd8a4fa42c7f"},"description":"Two Ruby iterator methods I really love, thanks to its simplicity and beauty","breadcrumb":{"@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#primaryimage","url":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2013\/07\/two-powerful-iterator-methods-featured-image.png","contentUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2013\/07\/two-powerful-iterator-methods-featured-image.png","width":1024,"height":576},{"@type":"BreadcrumbList","@id":"https:\/\/www.altoros.com\/blog\/two-or-three-powerful-iterators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.altoros.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Two Powerful Iterator Methods"}]},{"@type":"WebSite","@id":"https:\/\/www.altoros.com\/blog\/#website","url":"https:\/\/www.altoros.com\/blog\/","name":"Altoros","description":"Insight","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.altoros.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.altoros.com\/blog\/#\/schema\/person\/019e8147b835bc8f1b4abd8a4fa42c7f","name":"Alena Vasilenko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2019\/06\/alena-vasilenko-author-e1561752194994-96x96.jpg","url":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2019\/06\/alena-vasilenko-author-e1561752194994-96x96.jpg","contentUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2019\/06\/alena-vasilenko-author-e1561752194994-96x96.jpg","caption":"Alena Vasilenko"},"description":"Alena Vasilenko is Communications Manager at Altoros. She has proven track record of supporting R&amp;D engineers in their research activities on such topics as big data and cloud computing, translating the research results into easy-to-understand stories.","url":"https:\/\/www.altoros.com\/blog\/author\/alena-vasilenko\/"}]}},"_links":{"self":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts\/47445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/comments?post=47445"}],"version-history":[{"count":23,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts\/47445\/revisions"}],"predecessor-version":[{"id":66001,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts\/47445\/revisions\/66001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/media\/58501"}],"wp:attachment":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/media?parent=47445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/categories?post=47445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/tags?post=47445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}