{"action":"created","comment":{"url":"https://api.github.com/repos/bbatsov/rubocop/pulls/comments/49098202","id":49098202,"diff_hunk":"@@ -0,0 +1,89 @@\n+# encoding: utf-8\n+\n+require 'spec_helper'\n+\n+describe RuboCop::Cop::Performance::DoubleStartEndWith do\n+  subject(:cop) { described_class.new }\n+\n+  before do\n+    inspect_source(cop, source)\n+  end\n+\n+  context 'two #start_with? calls' do\n+    context 'with the same receiver' do\n+      context 'all parameters of the second call are pure' do\n+        let(:source) { 'x.start_with?(a, b) || x.start_with?(\"c\", D)' }","path":"spec/rubocop/cop/performance/double_start_end_with_spec.rb","position":15,"original_position":15,"commit_id":"2f42e8794d6494f37f29e882854e216d9eb657ed","original_commit_id":"1bb60bb7622a2e63f243ecc7a0ea6609f605660c","user":{"login":"alexdowad","id":1067359,"avatar_url":"https://avatars.githubusercontent.com/u/1067359?v=3","gravatar_id":"","url":"https://api.github.com/users/alexdowad","html_url":"https://github.com/alexdowad","followers_url":"https://api.github.com/users/alexdowad/followers","following_url":"https://api.github.com/users/alexdowad/following{/other_user}","gists_url":"https://api.github.com/users/alexdowad/gists{/gist_id}","starred_url":"https://api.github.com/users/alexdowad/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexdowad/subscriptions","organizations_url":"https://api.github.com/users/alexdowad/orgs","repos_url":"https://api.github.com/users/alexdowad/repos","events_url":"https://api.github.com/users/alexdowad/events{/privacy}","received_events_url":"https://api.github.com/users/alexdowad/received_events","type":"User","site_admin":false},"body":"Hmm. Seems equally confusing with `pure?`, to be honest.","created_at":"2016-01-07T17:14:23Z","updated_at":"2016-01-07T17:14:23Z","html_url":"https://github.com/bbatsov/rubocop/pull/2590#discussion_r49098202","pull_request_url":"https://api.github.com/repos/bbatsov/rubocop/pulls/2590","_links":{"self":{"href":"https://api.github.com/repos/bbatsov/rubocop/pulls/comments/49098202"},"html":{"href":"https://github.com/bbatsov/rubocop/pull/2590#discussion_r49098202"},"pull_request":{"href":"https://api.github.com/repos/bbatsov/rubocop/pulls/2590"}}},"pull_request":{"url":"https://api.github.com/repos/bbatsov/rubocop/pulls/2590","id":55200110,"html_url":"https://github.com/bbatsov/rubocop/pull/2590","diff_url":"https://github.com/bbatsov/rubocop/pull/2590.diff","patch_url":"https://github.com/bbatsov/rubocop/pull/2590.patch","issue_url":"https://api.github.com/repos/bbatsov/rubocop/issues/2590","number":2590,"state":"open","locked":false,"title":"New Performance/DoubleStartEndWith cop","user":{"login":"DNNX","id":447953,"avatar_url":"https://avatars.githubusercontent.com/u/447953?v=3","gravatar_id":"","url":"https://api.github.com/users/DNNX","html_url":"https://github.com/DNNX","followers_url":"https://api.github.com/users/DNNX/followers","following_url":"https://api.github.com/users/DNNX/following{/other_user}","gists_url":"https://api.github.com/users/DNNX/gists{/gist_id}","starred_url":"https://api.github.com/users/DNNX/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DNNX/subscriptions","organizations_url":"https://api.github.com/users/DNNX/orgs","repos_url":"https://api.github.com/users/DNNX/repos","events_url":"https://api.github.com/users/DNNX/events{/privacy}","received_events_url":"https://api.github.com/users/DNNX/received_events","type":"User","site_admin":false},"body":"This is a continuation of https://github.com/bbatsov/rubocop/pull/2535 .\r\n\r\nThis cop checks for double `#start_with?` or `#end_with?` calls\r\nseparated by `||`. In some cases such calls can be replaced\r\nwith an single `#start_with?`/`#end_with?` call.\r\n\r\n```ruby\r\n  # bad\r\n  str.start_with?(\"a\") || str.start_with?(Some::CONST)\r\n  str.start_with?(\"a\", \"b\") || str.start_with?(\"c\")\r\n  var1 = ...\r\n  var2 = ...\r\n  str.end_with?(var1) || str.end_with?(var2)\r\n\r\n  # good\r\n  str.start_with?(\"a\", Some::CONST)\r\n  str.start_with?(\"a\", \"b\", \"c\")\r\n  var1 = ...\r\n  var2 = ...\r\n  str.end_with?(var1, var2)\r\n```\r\n\r\nThe corner cases are when arguments to start_with? have side effects,\r\nor when the receiver itself has side effects, or when the receiver\r\nis not a string at all, or when someone monkey-patched `String#start_with?`\r\n\r\nBenchmark results are not too convincing because of a big variance, but they show that the single-call approach is at least not slower that the two-call approach.\r\n\r\n```ruby\r\nprefix1 = \"lib/\"\r\nprefix2 = \"test/\"\r\n{\r\n  'First prefix matches' => 'lib/file.rb',\r\n  'Second prefix matches' => 'test/file.rb',\r\n  'None matches' => 'app/models/file.rb'\r\n}.each do |description, str|\r\n  Benchmark.ips do |x|\r\n    puts \"==== #{description} ====\"\r\n    x.report('two calls') { str.start_with?(prefix1) || str.start_with?(prefix2) }\r\n    x.report('one call')  { str.start_with?(prefix1, prefix2) }\r\n    x.compare!\r\n  end\r\nend\r\n```\r\n\r\n```\r\n==== First prefix matches ====\r\nCalculating -------------------------------------\r\n           two calls    31.010k i/100ms\r\n            one call    35.180k i/100ms\r\n-------------------------------------------------\r\n           two calls      2.035M (±14.6%) i/s -      9.644M\r\n            one call      2.039M (±15.9%) i/s -      9.569M\r\n\r\nComparison:\r\n            one call:  2038728.5 i/s\r\n           two calls:  2034701.2 i/s - 1.00x slower\r\n\r\n==== Second prefix matches ====\r\nCalculating -------------------------------------\r\n           two calls    31.139k i/100ms\r\n            one call    34.518k i/100ms\r\n-------------------------------------------------\r\n           two calls      1.515M (±20.7%) i/s -      6.944M\r\n            one call      1.709M (±19.5%) i/s -      7.836M\r\n\r\nComparison:\r\n            one call:  1708570.7 i/s\r\n           two calls:  1514975.3 i/s - 1.13x slower\r\n\r\n==== None matches ====\r\nCalculating -------------------------------------\r\n           two calls    33.038k i/100ms\r\n            one call    36.789k i/100ms\r\n-------------------------------------------------\r\n           two calls      1.471M (±18.9%) i/s -      6.773M\r\n            one call      1.778M (±19.9%) i/s -      8.130M\r\n\r\nComparison:\r\n            one call:  1778026.2 i/s\r\n           two calls:  1471409.3 i/s - 1.21x slower\r\n```\r\n\r\nWild life occurrences: https://github.com/ManageIQ/manageiq/pull/5558, https://github.com/rails/rails/pull/22374, the aforementioned https://github.com/bbatsov/rubocop/pull/2535.\r\n","created_at":"2016-01-06T10:52:23Z","updated_at":"2016-01-07T17:14:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"45aaf95a5bcdde1354d6760c4f2db6e5c0d44b00","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/bbatsov/rubocop/pulls/2590/commits","review_comments_url":"https://api.github.com/repos/bbatsov/rubocop/pulls/2590/comments","review_comment_url":"https://api.github.com/repos/bbatsov/rubocop/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bbatsov/rubocop/issues/2590/comments","statuses_url":"https://api.github.com/repos/bbatsov/rubocop/statuses/2f42e8794d6494f37f29e882854e216d9eb657ed","head":{"label":"DNNX:double-start-end-with","ref":"double-start-end-with","sha":"2f42e8794d6494f37f29e882854e216d9eb657ed","user":{"login":"DNNX","id":447953,"avatar_url":"https://avatars.githubusercontent.com/u/447953?v=3","gravatar_id":"","url":"https://api.github.com/users/DNNX","html_url":"https://github.com/DNNX","followers_url":"https://api.github.com/users/DNNX/followers","following_url":"https://api.github.com/users/DNNX/following{/other_user}","gists_url":"https://api.github.com/users/DNNX/gists{/gist_id}","starred_url":"https://api.github.com/users/DNNX/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DNNX/subscriptions","organizations_url":"https://api.github.com/users/DNNX/orgs","repos_url":"https://api.github.com/users/DNNX/repos","events_url":"https://api.github.com/users/DNNX/events{/privacy}","received_events_url":"https://api.github.com/users/DNNX/received_events","type":"User","site_admin":false},"repo":{"id":48509073,"name":"rubocop","full_name":"DNNX/rubocop","owner":{"login":"DNNX","id":447953,"avatar_url":"https://avatars.githubusercontent.com/u/447953?v=3","gravatar_id":"","url":"https://api.github.com/users/DNNX","html_url":"https://github.com/DNNX","followers_url":"https://api.github.com/users/DNNX/followers","following_url":"https://api.github.com/users/DNNX/following{/other_user}","gists_url":"https://api.github.com/users/DNNX/gists{/gist_id}","starred_url":"https://api.github.com/users/DNNX/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DNNX/subscriptions","organizations_url":"https://api.github.com/users/DNNX/orgs","repos_url":"https://api.github.com/users/DNNX/repos","events_url":"https://api.github.com/users/DNNX/events{/privacy}","received_events_url":"https://api.github.com/users/DNNX/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/DNNX/rubocop","description":"A Ruby static code analyzer, based on the community Ruby style guide.","fork":true,"url":"https://api.github.com/repos/DNNX/rubocop","forks_url":"https://api.github.com/repos/DNNX/rubocop/forks","keys_url":"https://api.github.com/repos/DNNX/rubocop/keys{/key_id}","collaborators_url":"https://api.github.com/repos/DNNX/rubocop/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/DNNX/rubocop/teams","hooks_url":"https://api.github.com/repos/DNNX/rubocop/hooks","issue_events_url":"https://api.github.com/repos/DNNX/rubocop/issues/events{/number}","events_url":"https://api.github.com/repos/DNNX/rubocop/events","assignees_url":"https://api.github.com/repos/DNNX/rubocop/assignees{/user}","branches_url":"https://api.github.com/repos/DNNX/rubocop/branches{/branch}","tags_url":"https://api.github.com/repos/DNNX/rubocop/tags","blobs_url":"https://api.github.com/repos/DNNX/rubocop/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/DNNX/rubocop/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/DNNX/rubocop/git/refs{/sha}","trees_url":"https://api.github.com/repos/DNNX/rubocop/git/trees{/sha}","statuses_url":"https://api.github.com/repos/DNNX/rubocop/statuses/{sha}","languages_url":"https://api.github.com/repos/DNNX/rubocop/languages","stargazers_url":"https://api.github.com/repos/DNNX/rubocop/stargazers","contributors_url":"https://api.github.com/repos/DNNX/rubocop/contributors","subscribers_url":"https://api.github.com/repos/DNNX/rubocop/subscribers","subscription_url":"https://api.github.com/repos/DNNX/rubocop/subscription","commits_url":"https://api.github.com/repos/DNNX/rubocop/commits{/sha}","git_commits_url":"https://api.github.com/repos/DNNX/rubocop/git/commits{/sha}","comments_url":"https://api.github.com/repos/DNNX/rubocop/comments{/number}","issue_comment_url":"https://api.github.com/repos/DNNX/rubocop/issues/comments{/number}","contents_url":"https://api.github.com/repos/DNNX/rubocop/contents/{+path}","compare_url":"https://api.github.com/repos/DNNX/rubocop/compare/{base}...{head}","merges_url":"https://api.github.com/repos/DNNX/rubocop/merges","archive_url":"https://api.github.com/repos/DNNX/rubocop/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/DNNX/rubocop/downloads","issues_url":"https://api.github.com/repos/DNNX/rubocop/issues{/number}","pulls_url":"https://api.github.com/repos/DNNX/rubocop/pulls{/number}","milestones_url":"https://api.github.com/repos/DNNX/rubocop/milestones{/number}","notifications_url":"https://api.github.com/repos/DNNX/rubocop/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/DNNX/rubocop/labels{/name}","releases_url":"https://api.github.com/repos/DNNX/rubocop/releases{/id}","created_at":"2015-12-23T20:09:52Z","updated_at":"2015-12-23T20:09:54Z","pushed_at":"2016-01-07T14:50:09Z","git_url":"git://github.com/DNNX/rubocop.git","ssh_url":"git@github.com:DNNX/rubocop.git","clone_url":"https://github.com/DNNX/rubocop.git","svn_url":"https://github.com/DNNX/rubocop","homepage":"","size":9975,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"bbatsov:master","ref":"master","sha":"b3a9d2fc34fd2dd629b667395f40822aaef89fd0","user":{"login":"bbatsov","id":103882,"avatar_url":"https://avatars.githubusercontent.com/u/103882?v=3","gravatar_id":"","url":"https://api.github.com/users/bbatsov","html_url":"https://github.com/bbatsov","followers_url":"https://api.github.com/users/bbatsov/followers","following_url":"https://api.github.com/users/bbatsov/following{/other_user}","gists_url":"https://api.github.com/users/bbatsov/gists{/gist_id}","starred_url":"https://api.github.com/users/bbatsov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbatsov/subscriptions","organizations_url":"https://api.github.com/users/bbatsov/orgs","repos_url":"https://api.github.com/users/bbatsov/repos","events_url":"https://api.github.com/users/bbatsov/events{/privacy}","received_events_url":"https://api.github.com/users/bbatsov/received_events","type":"User","site_admin":false},"repo":{"id":4095287,"name":"rubocop","full_name":"bbatsov/rubocop","owner":{"login":"bbatsov","id":103882,"avatar_url":"https://avatars.githubusercontent.com/u/103882?v=3","gravatar_id":"","url":"https://api.github.com/users/bbatsov","html_url":"https://github.com/bbatsov","followers_url":"https://api.github.com/users/bbatsov/followers","following_url":"https://api.github.com/users/bbatsov/following{/other_user}","gists_url":"https://api.github.com/users/bbatsov/gists{/gist_id}","starred_url":"https://api.github.com/users/bbatsov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bbatsov/subscriptions","organizations_url":"https://api.github.com/users/bbatsov/orgs","repos_url":"https://api.github.com/users/bbatsov/repos","events_url":"https://api.github.com/users/bbatsov/events{/privacy}","received_events_url":"https://api.github.com/users/bbatsov/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bbatsov/rubocop","description":"A Ruby static code analyzer, based on the community Ruby style guide.","fork":false,"url":"https://api.github.com/repos/bbatsov/rubocop","forks_url":"https://api.github.com/repos/bbatsov/rubocop/forks","keys_url":"https://api.github.com/repos/bbatsov/rubocop/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bbatsov/rubocop/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bbatsov/rubocop/teams","hooks_url":"https://api.github.com/repos/bbatsov/rubocop/hooks","issue_events_url":"https://api.github.com/repos/bbatsov/rubocop/issues/events{/number}","events_url":"https://api.github.com/repos/bbatsov/rubocop/events","assignees_url":"https://api.github.com/repos/bbatsov/rubocop/assignees{/user}","branches_url":"https://api.github.com/repos/bbatsov/rubocop/branches{/branch}","tags_url":"https://api.github.com/repos/bbatsov/rubocop/tags","blobs_url":"https://api.github.com/repos/bbatsov/rubocop/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bbatsov/rubocop/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bbatsov/rubocop/git/refs{/sha}","trees_url":"https://api.github.com/repos/bbatsov/rubocop/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bbatsov/rubocop/statuses/{sha}","languages_url":"https://api.github.com/repos/bbatsov/rubocop/languages","stargazers_url":"https://api.github.com/repos/bbatsov/rubocop/stargazers","contributors_url":"https://api.github.com/repos/bbatsov/rubocop/contributors","subscribers_url":"https://api.github.com/repos/bbatsov/rubocop/subscribers","subscription_url":"https://api.github.com/repos/bbatsov/rubocop/subscription","commits_url":"https://api.github.com/repos/bbatsov/rubocop/commits{/sha}","git_commits_url":"https://api.github.com/repos/bbatsov/rubocop/git/commits{/sha}","comments_url":"https://api.github.com/repos/bbatsov/rubocop/comments{/number}","issue_comment_url":"https://api.github.com/repos/bbatsov/rubocop/issues/comments{/number}","contents_url":"https://api.github.com/repos/bbatsov/rubocop/contents/{+path}","compare_url":"https://api.github.com/repos/bbatsov/rubocop/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bbatsov/rubocop/merges","archive_url":"https://api.github.com/repos/bbatsov/rubocop/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bbatsov/rubocop/downloads","issues_url":"https://api.github.com/repos/bbatsov/rubocop/issues{/number}","pulls_url":"https://api.github.com/repos/bbatsov/rubocop/pulls{/number}","milestones_url":"https://api.github.com/repos/bbatsov/rubocop/milestones{/number}","notifications_url":"https://api.github.com/repos/bbatsov/rubocop/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bbatsov/rubocop/labels{/name}","releases_url":"https://api.github.com/repos/bbatsov/rubocop/releases{/id}","created_at":"2012-04-21T10:09:58Z","updated_at":"2016-01-07T08:14:50Z","pushed_at":"2016-01-07T16:39:28Z","git_url":"git://github.com/bbatsov/rubocop.git","ssh_url":"git@github.com:bbatsov/rubocop.git","clone_url":"https://github.com/bbatsov/rubocop.git","svn_url":"https://github.com/bbatsov/rubocop","homepage":"","size":13873,"stargazers_count":5636,"watchers_count":5636,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":700,"mirror_url":null,"open_issues_count":36,"forks":700,"open_issues":36,"watchers":5636,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bbatsov/rubocop/pulls/2590"},"html":{"href":"https://github.com/bbatsov/rubocop/pull/2590"},"issue":{"href":"https://api.github.com/repos/bbatsov/rubocop/issues/2590"},"comments":{"href":"https://api.github.com/repos/bbatsov/rubocop/issues/2590/comments"},"review_comments":{"href":"https://api.github.com/repos/bbatsov/rubocop/pulls/2590/comments"},"review_comment":{"href":"https://api.github.com/repos/bbatsov/rubocop/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bbatsov/rubocop/pulls/2590/commits"},"statuses":{"href":"https://api.github.com/repos/bbatsov/rubocop/statuses/2f42e8794d6494f37f29e882854e216d9eb657ed"}}}}