File: helpers.rb

package info (click to toggle)
ruby-ae 1.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 208 kB
  • sloc: ruby: 936; makefile: 2
file content (198 lines) | stat: -rw-r--r-- 3,479 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Nearly all, if not all, of these core extension are available from Ruby Facets.

# hack
NoArgument = Object.new

module Kernel
  # Is literally true.
  def true?
    TrueClass === self
  end

  # Is literally false.
  def false?
    FalseClass === self
  end

  # Are identical, eg. object_id's are equal.
  def identical?(exp)
    exp.object_id == object_id
  end

  # Alias for #identical?
  alias_method :identical_to?, :identical?

  # Word form of #==. Also can take a block.
  def eq?(value=NoArgument) #:yield:
    if block_given?
      self == yield
    else
      self == value
    end
  end

  # Word form of #===. Also can take a block.
  def case?(value=NoArgument) #:yield:
    if block_given?
      self === yield
    else
      self === value
    end
  end

  # Word form for #=~. Also can take a block.
  def match?(value=NoArgument)
    if block_given?
      self =~ yield
    else
      self =~ value
    end
  end

  # Broad equality.
  def equate?(x)
    equal?(x) || eql?(x) || self == x || self === x
  end

  # Can a message be sent to the receiver successfully?
  def send?(method, *args, &block)
    begin
      __send__(method, *args, &block)
      true
    rescue NoMethodError
      false
    end
  end

  #
  #def returns?(value) #:yield:
  #  value == yield
  #end

  unless method_defined?(:public_send)
    #
    def public_send(m,*a,&b)
      raise NoMethodError unless respond_to?(m)
      __send__(m,*a,&b)
    end
  end
end


class Object
  # Allows equal? to take a block.
  def equal?(value=NoArgument) #:yield:
    if block_given?
      super(yield)
    else
      super
    end
  end

  # Allows eql? to take a block.
  def eql?(value=NoArgument) #:yield:
    if block_given?
      super(yield)
    else
      super
    end
  end
end

class Numeric
  # Is self and given number within delta tolerance.
  #
  #   0.05.in_delta?(50000.0 / 10**6, 0.00001)
  #
  def in_delta?(orig, delta=0.001)
    #(num.to_f - to_f).abs <= delta.to_f
    delta >= (orig - self).abs
  end

  # Alias for #in_delta.
  alias_method :close?, :in_delta?

  # Verify epsilon tolerance.
  def in_epsilon?(orig, epsilon=0.001)
    in_delta?(orig, [orig, self].min * epsilon)
  end
end


class Module
  # Is a given class or module an ancestor of this
  # class or module?
  #
  #  class X ; end
  #  class Y < X ; end
  #
  #   Y.is?(X)  #=> true
  #
  def is?(base)
    Module===base && ancestors.slice(1..-1).include?(base)
  end
end

class Proc
  #
  def raises?(exception=Exception, *args)
    begin
      call(*args)
      false
    rescue exception => error
      exception === error
    end
  end

  #
  def throws?(sym, *args)
    catch(sym) do
      begin
        call(*args)
      rescue ArgumentError  # 1.9 exception
      rescue NameError      # 1.8 exception
      end
      return false
    end
    return true
  end

  # TODO: Put in facets?
  # TODO: wrong place, change yield?
  def change?
    pre_result = yield
    called = call
    post_result = yield
    pre_result != post_result
  end
end

class Symbol
  # Does the block throw this symbol?
  #
  def thrown?(*args)
    catch(self) do
      begin
        yield(*args)
      rescue ArgumentError  # 1.9 exception
      rescue NameError      # 1.8 exception
      end
      return false
    end
    return true
  end
end

class Exception
  #
  def self.raised? #:yeild:
    begin
      yield
      false
    rescue self
      true
    end
  end
end

# Copyright (c) 2008,2009 Thomas Sawyer