Module: AwesomeMethodArray

Defined in:
lib/amazing_print/core_ext/awesome_method_array.rb

Overview

Copyright © 2010-2016 Michael Dvorkin and contributors

AmazingPrint is freely distributable under the terms of MIT license. See LICENSE file or www.opensource.org/licenses/mit-license.php


The following makes it possible to invoke amazing_print while performing operations on method arrays, ex:

ap [].methods - Object.methods
ap ''.methods.grep(/!|\?/)

If you could think of a better way please let me know :-)

Instance Method Summary collapse

Instance Method Details

#&(other) ⇒ Object



24
25
26
27
28
# File 'lib/amazing_print/core_ext/awesome_method_array.rb', line 24

def &(other)
  super.tap do |arr|
    arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
  end
end

#-(other) ⇒ Object

:nodoc:



18
19
20
21
22
# File 'lib/amazing_print/core_ext/awesome_method_array.rb', line 18

def -(other)
  super.tap do |arr|
    arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
  end
end

#grep(pattern, &blk) ⇒ Object

Intercepting Array#grep needs a special treatment since grep accepts an optional block.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/amazing_print/core_ext/awesome_method_array.rb', line 34

def grep(pattern, &blk)
  #
  # The following looks rather insane and I've sent numerous hours trying
  # to figure it out. The problem is that if grep gets called with the
  # block, for example:
  #
  #    [].methods.grep(/(.+?)_by/) { $1.to_sym }
  #
  # ...then simple:
  #
  #    original_grep(pattern, &blk)
  #
  # doesn't set $1 within the grep block which causes nil.to_sym failure.
  # The workaround below has been tested with Ruby 1.8.7/Rails 2.3.8 and
  # Ruby 1.9.2/Rails 3.0.0. For more info see the following thread dating
  # back to 2003 when Ruby 1.8.0 was as fresh off the grill as Ruby 1.9.2
  # is in 2010 :-)
  #
  # http://www.justskins.com/forums/bug-when-rerouting-string-52852.html
  #
  # BTW, if you figure out a better way of intercepting Array#grep please
  # let me know: twitter.com/mid -- or just say hi so I know you've read
  # the comment :-)
  #
  arr = if blk
          super(pattern) do |match|
            #
            # The binding can only be used with Ruby-defined methods, therefore
            # we must rescue potential "ArgumentError: Can't create Binding from
            # C level Proc" error.
            #
            # For example, the following raises ArgumentError since #succ method
            # is defined in C.
            #
            # [ 0, 1, 2, 3, 4 ].grep(1..2, &:succ)
            #
            begin
              eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding, __FILE__, __LINE__)
            rescue StandardError
              ArgumentError
            end
            yield match
          end
        else
          super(pattern)
        end
  arr.instance_variable_set(:@__awesome_methods__, instance_variable_get(:@__awesome_methods__))
  arr.select! { |item| item.is_a?(Symbol) || item.is_a?(String) } # grep block might return crap.
  arr
end