{"id":2925,"date":"2012-03-23T09:18:01","date_gmt":"2012-03-23T13:18:01","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=2925"},"modified":"2012-04-04T13:36:56","modified_gmt":"2012-04-04T17:36:56","slug":"simple-array-comparison-with-ruby","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby","title":{"rendered":"Simple Array Comparison with Ruby"},"content":{"rendered":"

I ran into a task where I needed to find the differences with some data. Since I’ve not spent much time with ruby I decided to give it a shot. It was surprisingly simple to do what I needed to do in ruby. I’m not going to post the entire details right now, but wanted to leave a quick note here on comparing arrays.<\/p>\n

I stored the data I needed to evaluate into an array. I wanted to find the items that appeared in array A but not in array B. Basically all we need to do is included the ruby set library and then it is very very simple to do this.<\/p>\n

\r\nrequire 'set'\r\n\r\n#create array\r\na = [1,2,3,4]\r\nb = [2,3,5,6]\r\n\r\n#convert to set\r\na.to_set\r\nb.to_set\r\n\r\n#remove items in b from a\r\ndiff = (a - b)\r\n\r\n#output what ever is left\r\nputs diff\r\n<\/pre>\n

Since I already had my data in an array, I just converted it to a set object. Technically if you are creating a set you could use either of these methods:<\/p>\n

\r\na = Set.new[1,2,3]\r\n#or\r\na = [1,2,3].to_set\r\n<\/pre>\n

Mainly I wanted to add this little snippet so I can refer back to this at some point, but I figured it may help someone else who is new to ruby. For more information on set check out the set documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

I ran into a task where I needed to find the differences with some data. Since I’ve not spent much time with ruby I decided to give it a shot. It was surprisingly simple to do what I needed to do in ruby. I’m not going to post the entire details right now, but wanted … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":2929,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[156,17],"tags":[153,155,152,154],"yoast_head":"\nSimple Array Comparison with Ruby<\/title>\n<meta name=\"description\" content=\"I ran into a task where I needed to find the differences with some data. Since I've not spent much time with ruby I decided to give it a shot. It was\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Johnathan Ward\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/teamtutorials.com\/wp-content\/uploads\/2012\/03\/256px-Ruby_logo.svg_.png\",\"contentUrl\":\"https:\/\/teamtutorials.com\/wp-content\/uploads\/2012\/03\/256px-Ruby_logo.svg_.png\",\"width\":\"256\",\"height\":\"256\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#webpage\",\"url\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby\",\"name\":\"Simple Array Comparison with Ruby\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#primaryimage\"},\"datePublished\":\"2012-03-23T13:18:01+00:00\",\"dateModified\":\"2012-04-04T17:36:56+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\"},\"description\":\"I ran into a task where I needed to find the differences with some data. Since I've not spent much time with ruby I decided to give it a shot. It was\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple Array Comparison with Ruby\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\",\"name\":\"Johnathan Ward\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"caption\":\"Johnathan Ward\"},\"description\":\"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.\",\"sameAs\":[\"http:\/\/johnathanward.com\",\"https:\/\/twitter.com\/spyderman4g63\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple Array Comparison with Ruby","description":"I ran into a task where I needed to find the differences with some data. Since I've not spent much time with ruby I decided to give it a shot. It was","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:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby","twitter_misc":{"Written by":"Johnathan Ward","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#primaryimage","inLanguage":"en-US","url":"https:\/\/teamtutorials.com\/wp-content\/uploads\/2012\/03\/256px-Ruby_logo.svg_.png","contentUrl":"https:\/\/teamtutorials.com\/wp-content\/uploads\/2012\/03\/256px-Ruby_logo.svg_.png","width":"256","height":"256"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#webpage","url":"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby","name":"Simple Array Comparison with Ruby","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#primaryimage"},"datePublished":"2012-03-23T13:18:01+00:00","dateModified":"2012-04-04T17:36:56+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9"},"description":"I ran into a task where I needed to find the differences with some data. Since I've not spent much time with ruby I decided to give it a shot. It was","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/simple-array-comparison-with-ruby#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"Simple Array Comparison with Ruby"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9","name":"Johnathan Ward","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","caption":"Johnathan Ward"},"description":"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.","sameAs":["http:\/\/johnathanward.com","https:\/\/twitter.com\/spyderman4g63"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/2925"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=2925"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/2925\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media\/2929"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=2925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=2925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=2925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}