{"id":17261,"date":"2017-02-20T17:17:13","date_gmt":"2017-02-20T22:17:13","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=17261"},"modified":"2017-02-21T00:03:23","modified_gmt":"2017-02-21T05:03:23","slug":"rubys-reject","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/02\/20\/rubys-reject\/","title":{"rendered":"Ruby&rsquo;s reject!"},"content":{"rendered":"<p><a href=\"http:\/\/accidentallyquadratic.tumblr.com\/post\/157496054437\/ruby-reject\">Accidentally Quadratic<\/a> (<a href=\"https:\/\/news.ycombinator.com\/item?id=13691303\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"http:\/\/accidentallyquadratic.tumblr.com\/post\/157496054437\/ruby-reject\">\n<p>The code used to be linear, but it regressed in response to <a href=\"https:\/\/bugs.ruby-lang.org\/issues\/2545\">bug #2545<\/a>, which concerned the behavior when the block passed to <code>reject!<\/code> executed a <code>break<\/code> or otherwise exited early. Because <code>reject!<\/code> is in-place, any partial modifications it makes are still visible after an early exit, and <code>reject!<\/code> was leaving the array in a nonsensical state. The obvious fix was to ensure that the array was <em>always<\/em> in a consistent state, which is what resulted in the &ldquo;delete every time&rdquo; behavior.<\/p>\n<p>I find this interesting as a cautionary tale of how several of Ruby&rsquo;s features (here, ubiquitous mutability, blocks, and nonlocal exits) interact to create suprising edge cases that need to be addressed, and how addressing those edge cases can easily result in yet more problems (here, quadratic performance).<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Accidentally Quadratic (Hacker News): The code used to be linear, but it regressed in response to bug #2545, which concerned the behavior when the block passed to reject! executed a break or otherwise exited early. Because reject! is in-place, any partial modifications it makes are still visible after an early exit, and reject! was leaving [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,138,71,287],"class_list":["post-17261","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-optimization","tag-programming","tag-ruby"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=17261"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17261\/revisions"}],"predecessor-version":[{"id":17269,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/17261\/revisions\/17269"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=17261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=17261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=17261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}