{"id":213813,"date":"2014-02-25T11:35:03","date_gmt":"2014-02-25T07:35:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=213813"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=213813","title":{"rendered":"<span class=\"post_title\">7 \u0442\u0440\u044e\u043a\u043e\u0432 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u0432 Ruby<\/span>"},"content":{"rendered":"<div class=\"content html_format\">       <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/385\/e04\/370\/385e043706761c5e71bd81b030dbf642.jpg\" alt=\"ruby arrays\"\/><\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0442\u0440\u044e\u043a\u0438 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u0432 Ruby. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u0435\u0441\u0442\u044c \u0420\u0443\u0431\u0438\u0414\u043e\u043a \u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0433\u0434\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432\u0441\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043d\u043e \u0437\u0434\u0435\u0441\u044c \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<br \/>  <a name=\"habracut\"><\/a><\/p>\n<h2>1. \u041a\u0430\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0438\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430<\/h2>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0438\u043b\u0438 \u0432\u0441\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0435-\u043c\u0435\u0439\u043b\u044b \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u043e\u0432:  <\/p>\n<pre><code class=\"ruby\">imported_emails = [ 'john@doe.com', 'janet@doe.com' ] existing_emails = [ 'john@doe.com', 'janet@doe.com' , 'fred@mercury.com' ]   puts 'already imported' if (imported_emails - existing_emails).empty? <\/code><\/pre>\n<p>  <b>\u0417\u0430\u043f\u0443\u0441\u043a \u0432 IRB<\/b>:<br \/>  <i>already imported<br \/>  =&gt; nil<\/i><\/p>\n<h2>2. \u041a\u0430\u043a \u043d\u0430\u0439\u0442\u0438 \u043e\u0431\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0434\u0432\u0443\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445<\/h2>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043e\u0431\u0449\u0438\u0435 \u0442\u0435\u0433\u0438 \u0432 \u0434\u0432\u0443\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0441\u0442\u0430\u0445:  <\/p>\n<pre><code class=\"ruby\">tags_post1 = [ 'ruby', 'rails', 'test' ] tags_post2 = [ 'test', 'rspec' ]   common_tags = tags_post1 & tags_post2 <\/code><\/pre>\n<p>  <b>\u0417\u0430\u043f\u0443\u0441\u043a \u0432 IRB<\/b>:<br \/>  <i>=&gt; [\u00abtest\u00bb]<\/i><\/p>\n<h2>3. \u041a\u0430\u043a \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0434\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043d\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b<\/h2>\n<p>  <\/p>\n<pre><code class=\"ruby\">followeds1 = [ 1, 2, 3 ] followeds2 = [ 2, 4, 5 ]   all_followeds =  followeds1 | followeds2 <\/code><\/pre>\n<p>  <b>\u0417\u0430\u043f\u0443\u0441\u043a \u0432 IRB<\/b>:<br \/>  <i>=&gt; [1, 2, 3, 4, 5]<\/i><\/p>\n<h2>4. \u041a\u0430\u043a \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0445\u0435\u0448\u0435\u0439<\/h2>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e API, \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0441\u0438\u0432\u0430 \u0445\u0435\u0448\u0435\u0439:  <\/p>\n<pre><code class=\"ruby\">data = [  {     name: 'Christophe',     location: 'Belgium'  },  {     name: 'John',     location: 'United States of America'  },  {     name: 'Piet',     location: 'Belgium'  },  {     name: 'Fran\u00e7ois',     location: 'France'  } ] <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438, \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u043f\u043e\u043b\u044e location, \u0442\u043e\u0433\u0434\u0430 \u0434\u0435\u043b\u0430\u0435\u043c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"ruby\">data.sort_by { |hsh| hsh[:location] } <\/code><\/pre>\n<p>  <b>\u0417\u0430\u043f\u0443\u0441\u043a \u0432 IRB<\/b>:<br \/>  <i>=&gt; [<br \/>   {:name=&gt;\u00abChristophe\u00bb, :location=&gt;\u00abBelgium\u00bb},<br \/>   {:name=&gt;\u00abPiet\u00bb, :location=&gt;\u00abBelgium\u00bb},<br \/>   {:name=&gt;\u00abFran\u00e7ois\u00bb, :location=&gt;\u00abFrance\u00bb},<br \/>   {:name=&gt;\u00abJohn\u00bb, :location=&gt;\u00abUnited States of America\u00bb}<br \/>   ]<\/i><\/p>\n<h2>5. \u041a\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0443<\/h2>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u044b \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435, \u043d\u043e \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u0440\u043e\u0434\u0443\u043a\u0442 \u0431\u0440\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u043e\u0434\u043d\u043e\u0439 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438:  <\/p>\n<pre><code class=\"ruby\">Product = Struct.new(:id, :category_id)   products = [  Product.new(1, 1),  Product.new(2, 2),  Product.new(3, 3),  Product.new(4, 1),  Product.new(5, 3),  Product.new(6, 5), ]   products = products.uniq &:category_id <\/code><\/pre>\n<p>  <b>\u0417\u0430\u043f\u0443\u0441\u043a \u0432 IRB<\/b>:<br \/>  <i>=&gt; [<br \/>   #&lt;struct Product id=1, category_id=1&gt;,<br \/>   #&lt;struct Product id=2, category_id=2&gt;,<br \/>   #&lt;struct Product id=3, category_id=3&gt;,<br \/>   #&lt;struct Product id=6, category_id=5&gt;<br \/>   ]<br \/>  <\/i><\/p>\n<h2>6. \u041a\u0430\u043a \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430<\/h2>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443 \u0432\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u043a\u043d\u0438\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u043a\u0430\u043a\u043e\u043c\u0443-\u0442\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u043c\u0443 \u0441\u043b\u043e\u0432\u0443:  <\/p>\n<pre><code class=\"ruby\">books = [  'The Ruby Programming Language',  'Programming Ruby 1.9 & 2.0: The Pragmatic Programmers\\' Guide (The Facets of Ruby)',  'Practical Object-Oriented Design in Ruby: An Agile Primer',  'Eloquent Ruby',  'Ruby on Rails Tutorial: Learn Web Development with Rails' ]   books = books.grep(\/[Rr]ails\/) <\/code><\/pre>\n<p>  <b>\u0417\u0430\u043f\u0443\u0441\u043a \u0432 IRB<\/b>:<br \/>  <i>=&gt; [\u00abRuby on Rails Tutorial: Learn Web Development with Rails\u00bb]<\/i><\/p>\n<h2>7. \u041a\u0430\u043a \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432<\/h2>\n<p>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432 \u043b\u0438\u0431\u043e \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0434\u0443\u043a\u0442. \u0427\u0442\u043e\u0431\u044b \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u044b\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array() \u0438\u043b\u0438 [*]:  <\/p>\n<pre><code class=\"ruby\">def method  # \u2026    [*products] end <\/code><\/pre>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/213813\/\"> http:\/\/habrahabr.ru\/post\/213813\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">       <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/getpro\/habr\/post_images\/385\/e04\/370\/385e043706761c5e71bd81b030dbf642.jpg\" alt=\"ruby arrays\"\/><\/p>\n<p>  \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0442\u0440\u044e\u043a\u0438 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u0432 Ruby. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u0435\u0441\u0442\u044c \u0420\u0443\u0431\u0438\u0414\u043e\u043a \u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u0433\u0434\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432\u0441\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043d\u043e \u0437\u0434\u0435\u0441\u044c \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-213813","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/213813","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=213813"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/213813\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}