{"id":2576,"date":"2017-12-09T07:09:00","date_gmt":"2017-12-09T07:09:00","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2576"},"modified":"2020-01-09T07:54:42","modified_gmt":"2020-01-09T07:54:42","slug":"what-is-the-way-to-iterate-through-an-array-in-ruby","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/what-is-the-way-to-iterate-through-an-array-in-ruby\/","title":{"rendered":"What is the way to iterate through an array in Ruby?"},"content":{"rendered":"<p><strong>scmuser created the topic: What is the way to iterate through an array in Ruby?<\/strong><\/p>\n<p>What is the way to iterate through an array in Ruby?<\/p>\n<p>This will iterate through all the elements:<\/p>\n<p>[code language=&#8221;css&#8221;]<br \/>\narray = [1, 2, 3, 4, 5, 6]<br \/>\narray.each { |x| puts x }<\/p>\n<p>Prints:<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n[\/code]<\/p>\n<p>This will iterate through all the elements giving you the value and the index:<\/p>\n<p>[code language=&#8221;css&#8221;]<br \/>\narray = [&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;]<br \/>\narray.each_with_index {|val, index| puts &#8220;#{val} =&gt; #{index}&#8221; }<\/p>\n<p>Prints:<\/p>\n<p>A =&gt; 0<br \/>\nB =&gt; 1<br \/>\nC =&gt; 2<\/p>\n<p>[\/code]<\/p>\n<p>Source &#8211; <a href=\"http:\/\/stackoverflow.com\/questions\/310634\/what-...ugh-an-array-in-ruby\" target=\"_blank\" rel=\"noopener\">stackoverflow.com\/questions\/310634\/what-&#8230;ugh-an-array-in-ruby<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>scmuser created the topic: What is the way to iterate through an array in Ruby? What is the way to iterate through an array in Ruby? This will iterate through all the elements: [code language=&#8221;css&#8221;] array = [1, 2, 3, 4, 5, 6] array.each { |x| puts x } Prints: 1 2 3 4 5&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[178],"class_list":["post-2576","post","type-post","status-publish","format-standard","hentry","category-uncategorised","tag-ruby"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2576"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2576\/revisions"}],"predecessor-version":[{"id":8536,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2576\/revisions\/8536"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}