{"action":"created","comment":{"url":"https://api.github.com/repos/awesome-print/awesome_print/pulls/comments/68157039","id":68157039,"diff_hunk":"@@ -13,60 +13,137 @@ def initialize(array, inspector)\n       end\n \n       def format\n-        return \"[]\" if array == []\n-\n-        if array.instance_variable_defined?('@__awesome_methods__')\n-          methods_array(array)\n-        elsif options[:multiline]\n-          width = (array.size - 1).to_s.size\n-\n-          data = array.inject([]) do |arr, item|\n-            index = indent\n-            index << colorize(\"[#{arr.size.to_s.rjust(width)}] \", :array) if options[:index]\n-            indented do\n-              arr << (index << inspector.awesome(item))\n-            end\n-          end\n-\n-          data = limited(data, width) if should_be_limited?\n-          \"[\\n\" << data.join(\",\\n\") << \"\\n#{outdent}]\"\n+        case\n+        when array.empty?\n+          empty_array\n+        when awesome_array?\n+          methods_array\n+        when multiline_array?\n+          multiline_array\n         else\n-          \"[ \" << array.map{ |item| inspector.awesome(item) }.join(\", \") << \" ]\"\n+          simple_array\n         end\n       end\n \n       private\n \n-      def methods_array(a)\n-        a.sort! { |x, y| x.to_s <=> y.to_s }                  # Can't simply a.sort! because of o.methods << [ :blah ]\n-        object = a.instance_variable_get('@__awesome_methods__')\n-        tuples = a.map do |name|\n-          if name.is_a?(Symbol) || name.is_a?(String)         # Ignore garbage, ex. 42.methods << [ :blah ]\n-            tuple = if object.respond_to?(name, true)         # Is this a regular method?\n-              the_method = object.method(name) rescue nil     # Avoid potential ArgumentError if object#method is overridden.\n-              if the_method && the_method.respond_to?(:arity) # Is this original object#method?\n-                method_tuple(the_method)                      # Yes, we are good.\n-              end\n-            elsif object.respond_to?(:instance_method)              # Is this an unbound method?\n-              method_tuple(object.instance_method(name)) rescue nil # Rescue to avoid NameError when the method is not\n-            end                                                     # available (ex. File.lchmod on Ubuntu 12).\n+      def awesome_array?\n+        array.instance_variable_defined?('@__awesome_methods__')\n+      end\n+\n+      def multiline_array?\n+        options[:multiline]\n+      end\n+\n+      def empty_array\n+        \"[]\"\n+      end\n+\n+      def simple_array\n+        \"[ \" << array.map{ |item| inspector.awesome(item) }.join(\", \") << \" ]\"\n+      end\n+\n+      def multiline_array\n+        data = generate_printable_array\n+        data = limited(data, width(array)) if should_be_limited?\n+        data = data.join(\",\\n\")\n+\n+        \"[\\n#{data}\\n#{outdent}]\"\n+      end\n+\n+      def generate_printable_array\n+        array.map.with_index do |item, index|\n+          array_prefix(index, width(array)).tap do |temp|\n+            indented { temp << inspector.awesome(item) } \n           end\n-          tuple || [ name.to_s, '(?)', '?' ]                  # Return WTF default if all the above fails.\n         end\n+      end\n+\n+      def array_prefix(iteration, width)\n+        if options[:index]\n+          indent + colorize(\"[#{iteration.to_s.rjust(width)}] \", :array)\n+        else\n+          indent\n+        end\n+      end\n \n-        width = (tuples.size - 1).to_s.size\n-        name_width = tuples.map { |item| item[0].size }.max || 0\n-        args_width = tuples.map { |item| item[1].size }.max || 0\n+      def methods_array\n+        array.map!(&:to_s).sort!\n \n-        data = tuples.inject([]) do |arr, item|\n-          index = indent\n-          index << \"[#{arr.size.to_s.rjust(width)}]\" if @options[:index]\n-          indented do\n-            arr << \"#{index} #{colorize(item[0].rjust(name_width), :method)}#{colorize(item[1].ljust(args_width), :args)} #{colorize(item[2], :class)}\"\n+        data = generate_printable_tuples.join(\"\\n\")\n+\n+        \"[\\n#{data}\\n#{outdent}]\"\n+      end\n+\n+      def generate_printable_tuples\n+        tuples.map.with_index do |item, index|\n+          tuple_prefix(index, width(tuples)).tap do |temp|\n+            indented { temp << tuple_template(item) }\n           end\n         end\n+      end\n+\n+      def tuple_template(item)\n+        name_width, args_width = name_and_args_width\n+\n+        str = \"\"\n+        str += \"#{colorize(item[0].rjust(name_width), :method)}\"\n+        str += \"#{colorize(item[1].ljust(args_width), :args)} \"\n+        str += \"#{colorize(item[2], :class)}\"\n+      end\n+\n+      def tuples\n+        @tuples ||= array.map { |name| generate_tuple(name) }\n+      end\n+\n+      def name_and_args_width\n+        name_width, args_width = 0, 0\n+\n+        tuples.each do |item|\n+          name_width = [name_width, item[0].size].max\n+          args_width = [args_width, item[1].size].max\n+        end\n+\n+        return name_width, args_width\n+      end\n+\n+      def tuple_prefix(iteration, width)\n+        if options[:index]\n+          indent + colorize(\"[#{iteration.to_s.rjust(width)}] \", :array)\n+        else\n+          indent + \" \"\n+        end\n+      end\n+\n+      def generate_tuple(name)\n+        meth = case name\n+        when Symbol, String\n+          find_method(name)\n+        else\n+          nil\n+        end\n+\n+        meth ? method_tuple(meth) : [ name.to_s, '(?)', '?' ]\n+      end\n+\n+      def find_method(name)\n+        object = array.instance_variable_get('@__awesome_methods__')\n+\n+        meth = begin\n+          object.method(name)\n+        rescue NameError, ArgumentError\n+          nil\n+        end\n+","path":"lib/awesome_print/formatters/array_formatter.rb","position":165,"original_position":165,"commit_id":"dac0237df37870b4062f0597061055126638872d","original_commit_id":"dac0237df37870b4062f0597061055126638872d","user":{"login":"gerrywastaken","id":71481,"avatar_url":"https://avatars.githubusercontent.com/u/71481?v=3","gravatar_id":"","url":"https://api.github.com/users/gerrywastaken","html_url":"https://github.com/gerrywastaken","followers_url":"https://api.github.com/users/gerrywastaken/followers","following_url":"https://api.github.com/users/gerrywastaken/following{/other_user}","gists_url":"https://api.github.com/users/gerrywastaken/gists{/gist_id}","starred_url":"https://api.github.com/users/gerrywastaken/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gerrywastaken/subscriptions","organizations_url":"https://api.github.com/users/gerrywastaken/orgs","repos_url":"https://api.github.com/users/gerrywastaken/repos","events_url":"https://api.github.com/users/gerrywastaken/events{/privacy}","received_events_url":"https://api.github.com/users/gerrywastaken/received_events","type":"User","site_admin":false},"body":"Oh wow, I hadn't even considered that possibility! So this is for the case where an end user has created a method called `method` which doesn't return a `Method` object? 😆 \r\n\r\nI wonder how common that is and what lead @michaeldv to adding the check in the first place. Especially considering this code will no longer handle that condition and instead relies on method_tuple to realise it's not the right type.","created_at":"2016-06-23T00:03:14Z","updated_at":"2016-06-23T00:03:14Z","html_url":"https://github.com/awesome-print/awesome_print/pull/246#discussion_r68157039","pull_request_url":"https://api.github.com/repos/awesome-print/awesome_print/pulls/246","_links":{"self":{"href":"https://api.github.com/repos/awesome-print/awesome_print/pulls/comments/68157039"},"html":{"href":"https://github.com/awesome-print/awesome_print/pull/246#discussion_r68157039"},"pull_request":{"href":"https://api.github.com/repos/awesome-print/awesome_print/pulls/246"}}},"pull_request":{"url":"https://api.github.com/repos/awesome-print/awesome_print/pulls/246","id":73152887,"html_url":"https://github.com/awesome-print/awesome_print/pull/246","diff_url":"https://github.com/awesome-print/awesome_print/pull/246.diff","patch_url":"https://github.com/awesome-print/awesome_print/pull/246.patch","issue_url":"https://api.github.com/repos/awesome-print/awesome_print/issues/246","number":246,"state":"open","locked":false,"title":"Refactoring array formatter","user":{"login":"waldyr","id":1893997,"avatar_url":"https://avatars.githubusercontent.com/u/1893997?v=3","gravatar_id":"","url":"https://api.github.com/users/waldyr","html_url":"https://github.com/waldyr","followers_url":"https://api.github.com/users/waldyr/followers","following_url":"https://api.github.com/users/waldyr/following{/other_user}","gists_url":"https://api.github.com/users/waldyr/gists{/gist_id}","starred_url":"https://api.github.com/users/waldyr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/waldyr/subscriptions","organizations_url":"https://api.github.com/users/waldyr/orgs","repos_url":"https://api.github.com/users/waldyr/repos","events_url":"https://api.github.com/users/waldyr/events{/privacy}","received_events_url":"https://api.github.com/users/waldyr/received_events","type":"User","site_admin":false},"body":"Trying to move this beast from D to A. Any help, consideration or critic is welcome.\r\n\r\n@gerrywastaken @michaeldv What do you think? I'm going to hold this until I can safely benchmark.\r\n\r\n# TODO:\r\n- [x] Complex method #multiline_array.\r\n- [x] Complex method #tuples.\r\n- [ ] Limit array while processing.\r\n\r\n# Considerations:\r\n- [#array _prefix](https://github.com/awesome-print/awesome_print/pull/246/files#diff-d38e1087bf89bafb7e477570e614719bR62) and [#tuple_prefix](https://github.com/awesome-print/awesome_print/pull/246/files#diff-d38e1087bf89bafb7e477570e614719bR110) is almost the same method except for one space.\r\n- [limiting an array](https://github.com/awesome-print/awesome_print/pull/246/files#diff-d38e1087bf89bafb7e477570e614719bR48) could be way better if we limit while processing.\r\n- [#generate_printable_array](https://github.com/awesome-print/awesome_print/pull/246/files#diff-d38e1087bf89bafb7e477570e614719bR54) and [#generate_printable_tuples](https://github.com/awesome-print/awesome_print/pull/246/files#diff-d38e1087bf89bafb7e477570e614719bR78) is almost the same thing except for the prefix. The rest can be yielded.","created_at":"2016-06-09T03:28:54Z","updated_at":"2016-06-23T00:03:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5fae2bd4f1e4fa1b5ebf8454887c4221192b619e","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/awesome-print/awesome_print/pulls/246/commits","review_comments_url":"https://api.github.com/repos/awesome-print/awesome_print/pulls/246/comments","review_comment_url":"https://api.github.com/repos/awesome-print/awesome_print/pulls/comments{/number}","comments_url":"https://api.github.com/repos/awesome-print/awesome_print/issues/246/comments","statuses_url":"https://api.github.com/repos/awesome-print/awesome_print/statuses/dac0237df37870b4062f0597061055126638872d","head":{"label":"awesome-print:fix/refactor-array-formatter","ref":"fix/refactor-array-formatter","sha":"dac0237df37870b4062f0597061055126638872d","user":{"login":"awesome-print","id":19637607,"avatar_url":"https://avatars.githubusercontent.com/u/19637607?v=3","gravatar_id":"","url":"https://api.github.com/users/awesome-print","html_url":"https://github.com/awesome-print","followers_url":"https://api.github.com/users/awesome-print/followers","following_url":"https://api.github.com/users/awesome-print/following{/other_user}","gists_url":"https://api.github.com/users/awesome-print/gists{/gist_id}","starred_url":"https://api.github.com/users/awesome-print/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awesome-print/subscriptions","organizations_url":"https://api.github.com/users/awesome-print/orgs","repos_url":"https://api.github.com/users/awesome-print/repos","events_url":"https://api.github.com/users/awesome-print/events{/privacy}","received_events_url":"https://api.github.com/users/awesome-print/received_events","type":"Organization","site_admin":false},"repo":{"id":592743,"name":"awesome_print","full_name":"awesome-print/awesome_print","owner":{"login":"awesome-print","id":19637607,"avatar_url":"https://avatars.githubusercontent.com/u/19637607?v=3","gravatar_id":"","url":"https://api.github.com/users/awesome-print","html_url":"https://github.com/awesome-print","followers_url":"https://api.github.com/users/awesome-print/followers","following_url":"https://api.github.com/users/awesome-print/following{/other_user}","gists_url":"https://api.github.com/users/awesome-print/gists{/gist_id}","starred_url":"https://api.github.com/users/awesome-print/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awesome-print/subscriptions","organizations_url":"https://api.github.com/users/awesome-print/orgs","repos_url":"https://api.github.com/users/awesome-print/repos","events_url":"https://api.github.com/users/awesome-print/events{/privacy}","received_events_url":"https://api.github.com/users/awesome-print/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/awesome-print/awesome_print","description":"Pretty print your Ruby objects with style -- in full color and with proper indentation","fork":false,"url":"https://api.github.com/repos/awesome-print/awesome_print","forks_url":"https://api.github.com/repos/awesome-print/awesome_print/forks","keys_url":"https://api.github.com/repos/awesome-print/awesome_print/keys{/key_id}","collaborators_url":"https://api.github.com/repos/awesome-print/awesome_print/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/awesome-print/awesome_print/teams","hooks_url":"https://api.github.com/repos/awesome-print/awesome_print/hooks","issue_events_url":"https://api.github.com/repos/awesome-print/awesome_print/issues/events{/number}","events_url":"https://api.github.com/repos/awesome-print/awesome_print/events","assignees_url":"https://api.github.com/repos/awesome-print/awesome_print/assignees{/user}","branches_url":"https://api.github.com/repos/awesome-print/awesome_print/branches{/branch}","tags_url":"https://api.github.com/repos/awesome-print/awesome_print/tags","blobs_url":"https://api.github.com/repos/awesome-print/awesome_print/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/awesome-print/awesome_print/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/awesome-print/awesome_print/git/refs{/sha}","trees_url":"https://api.github.com/repos/awesome-print/awesome_print/git/trees{/sha}","statuses_url":"https://api.github.com/repos/awesome-print/awesome_print/statuses/{sha}","languages_url":"https://api.github.com/repos/awesome-print/awesome_print/languages","stargazers_url":"https://api.github.com/repos/awesome-print/awesome_print/stargazers","contributors_url":"https://api.github.com/repos/awesome-print/awesome_print/contributors","subscribers_url":"https://api.github.com/repos/awesome-print/awesome_print/subscribers","subscription_url":"https://api.github.com/repos/awesome-print/awesome_print/subscription","commits_url":"https://api.github.com/repos/awesome-print/awesome_print/commits{/sha}","git_commits_url":"https://api.github.com/repos/awesome-print/awesome_print/git/commits{/sha}","comments_url":"https://api.github.com/repos/awesome-print/awesome_print/comments{/number}","issue_comment_url":"https://api.github.com/repos/awesome-print/awesome_print/issues/comments{/number}","contents_url":"https://api.github.com/repos/awesome-print/awesome_print/contents/{+path}","compare_url":"https://api.github.com/repos/awesome-print/awesome_print/compare/{base}...{head}","merges_url":"https://api.github.com/repos/awesome-print/awesome_print/merges","archive_url":"https://api.github.com/repos/awesome-print/awesome_print/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/awesome-print/awesome_print/downloads","issues_url":"https://api.github.com/repos/awesome-print/awesome_print/issues{/number}","pulls_url":"https://api.github.com/repos/awesome-print/awesome_print/pulls{/number}","milestones_url":"https://api.github.com/repos/awesome-print/awesome_print/milestones{/number}","notifications_url":"https://api.github.com/repos/awesome-print/awesome_print/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/awesome-print/awesome_print/labels{/name}","releases_url":"https://api.github.com/repos/awesome-print/awesome_print/releases{/id}","deployments_url":"https://api.github.com/repos/awesome-print/awesome_print/deployments","created_at":"2010-04-03T04:45:43Z","updated_at":"2016-06-22T10:26:37Z","pushed_at":"2016-06-21T14:54:23Z","git_url":"git://github.com/awesome-print/awesome_print.git","ssh_url":"git@github.com:awesome-print/awesome_print.git","clone_url":"https://github.com/awesome-print/awesome_print.git","svn_url":"https://github.com/awesome-print/awesome_print","homepage":"http://github.com/michaeldv/awesome_print","size":786,"stargazers_count":2785,"watchers_count":2785,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":218,"mirror_url":null,"open_issues_count":49,"forks":218,"open_issues":49,"watchers":2785,"default_branch":"master"}},"base":{"label":"awesome-print:master","ref":"master","sha":"10a815fd1a3f22900573fa25ce685df758726223","user":{"login":"awesome-print","id":19637607,"avatar_url":"https://avatars.githubusercontent.com/u/19637607?v=3","gravatar_id":"","url":"https://api.github.com/users/awesome-print","html_url":"https://github.com/awesome-print","followers_url":"https://api.github.com/users/awesome-print/followers","following_url":"https://api.github.com/users/awesome-print/following{/other_user}","gists_url":"https://api.github.com/users/awesome-print/gists{/gist_id}","starred_url":"https://api.github.com/users/awesome-print/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awesome-print/subscriptions","organizations_url":"https://api.github.com/users/awesome-print/orgs","repos_url":"https://api.github.com/users/awesome-print/repos","events_url":"https://api.github.com/users/awesome-print/events{/privacy}","received_events_url":"https://api.github.com/users/awesome-print/received_events","type":"Organization","site_admin":false},"repo":{"id":592743,"name":"awesome_print","full_name":"awesome-print/awesome_print","owner":{"login":"awesome-print","id":19637607,"avatar_url":"https://avatars.githubusercontent.com/u/19637607?v=3","gravatar_id":"","url":"https://api.github.com/users/awesome-print","html_url":"https://github.com/awesome-print","followers_url":"https://api.github.com/users/awesome-print/followers","following_url":"https://api.github.com/users/awesome-print/following{/other_user}","gists_url":"https://api.github.com/users/awesome-print/gists{/gist_id}","starred_url":"https://api.github.com/users/awesome-print/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/awesome-print/subscriptions","organizations_url":"https://api.github.com/users/awesome-print/orgs","repos_url":"https://api.github.com/users/awesome-print/repos","events_url":"https://api.github.com/users/awesome-print/events{/privacy}","received_events_url":"https://api.github.com/users/awesome-print/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/awesome-print/awesome_print","description":"Pretty print your Ruby objects with style -- in full color and with proper indentation","fork":false,"url":"https://api.github.com/repos/awesome-print/awesome_print","forks_url":"https://api.github.com/repos/awesome-print/awesome_print/forks","keys_url":"https://api.github.com/repos/awesome-print/awesome_print/keys{/key_id}","collaborators_url":"https://api.github.com/repos/awesome-print/awesome_print/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/awesome-print/awesome_print/teams","hooks_url":"https://api.github.com/repos/awesome-print/awesome_print/hooks","issue_events_url":"https://api.github.com/repos/awesome-print/awesome_print/issues/events{/number}","events_url":"https://api.github.com/repos/awesome-print/awesome_print/events","assignees_url":"https://api.github.com/repos/awesome-print/awesome_print/assignees{/user}","branches_url":"https://api.github.com/repos/awesome-print/awesome_print/branches{/branch}","tags_url":"https://api.github.com/repos/awesome-print/awesome_print/tags","blobs_url":"https://api.github.com/repos/awesome-print/awesome_print/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/awesome-print/awesome_print/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/awesome-print/awesome_print/git/refs{/sha}","trees_url":"https://api.github.com/repos/awesome-print/awesome_print/git/trees{/sha}","statuses_url":"https://api.github.com/repos/awesome-print/awesome_print/statuses/{sha}","languages_url":"https://api.github.com/repos/awesome-print/awesome_print/languages","stargazers_url":"https://api.github.com/repos/awesome-print/awesome_print/stargazers","contributors_url":"https://api.github.com/repos/awesome-print/awesome_print/contributors","subscribers_url":"https://api.github.com/repos/awesome-print/awesome_print/subscribers","subscription_url":"https://api.github.com/repos/awesome-print/awesome_print/subscription","commits_url":"https://api.github.com/repos/awesome-print/awesome_print/commits{/sha}","git_commits_url":"https://api.github.com/repos/awesome-print/awesome_print/git/commits{/sha}","comments_url":"https://api.github.com/repos/awesome-print/awesome_print/comments{/number}","issue_comment_url":"https://api.github.com/repos/awesome-print/awesome_print/issues/comments{/number}","contents_url":"https://api.github.com/repos/awesome-print/awesome_print/contents/{+path}","compare_url":"https://api.github.com/repos/awesome-print/awesome_print/compare/{base}...{head}","merges_url":"https://api.github.com/repos/awesome-print/awesome_print/merges","archive_url":"https://api.github.com/repos/awesome-print/awesome_print/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/awesome-print/awesome_print/downloads","issues_url":"https://api.github.com/repos/awesome-print/awesome_print/issues{/number}","pulls_url":"https://api.github.com/repos/awesome-print/awesome_print/pulls{/number}","milestones_url":"https://api.github.com/repos/awesome-print/awesome_print/milestones{/number}","notifications_url":"https://api.github.com/repos/awesome-print/awesome_print/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/awesome-print/awesome_print/labels{/name}","releases_url":"https://api.github.com/repos/awesome-print/awesome_print/releases{/id}","deployments_url":"https://api.github.com/repos/awesome-print/awesome_print/deployments","created_at":"2010-04-03T04:45:43Z","updated_at":"2016-06-22T10:26:37Z","pushed_at":"2016-06-21T14:54:23Z","git_url":"git://github.com/awesome-print/awesome_print.git","ssh_url":"git@github.com:awesome-print/awesome_print.git","clone_url":"https://github.com/awesome-print/awesome_print.git","svn_url":"https://github.com/awesome-print/awesome_print","homepage":"http://github.com/michaeldv/awesome_print","size":786,"stargazers_count":2785,"watchers_count":2785,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":218,"mirror_url":null,"open_issues_count":49,"forks":218,"open_issues":49,"watchers":2785,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/awesome-print/awesome_print/pulls/246"},"html":{"href":"https://github.com/awesome-print/awesome_print/pull/246"},"issue":{"href":"https://api.github.com/repos/awesome-print/awesome_print/issues/246"},"comments":{"href":"https://api.github.com/repos/awesome-print/awesome_print/issues/246/comments"},"review_comments":{"href":"https://api.github.com/repos/awesome-print/awesome_print/pulls/246/comments"},"review_comment":{"href":"https://api.github.com/repos/awesome-print/awesome_print/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/awesome-print/awesome_print/pulls/246/commits"},"statuses":{"href":"https://api.github.com/repos/awesome-print/awesome_print/statuses/dac0237df37870b4062f0597061055126638872d"}}}}
{"actor":{"display_login":"gerrywastaken"}}