Class: Object

Inherits:
BasicObject
Includes:
Redwood::LogsStuff
Defined in:
lib/sup.rb,
lib/sup/util.rb,
lib/sup/logger/singleton.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject



201
202
203
204
205
206
207
208
209
210
# File 'lib/sup/util.rb', line 201

def ancestors
  ret = []
  klass = self.class

  until klass == Object
    ret << klass
    klass = klass.superclass
  end
  ret
end

#benchmark(s, &b) ⇒ Object



259
260
261
262
263
264
# File 'lib/sup/util.rb', line 259

def benchmark s, &b
  ret = nil
  times = Benchmark.measure { ret = b.call }
  debug "benchmark #{s}: #{times}"
  ret
end

#idObject

this is for debugging purposes because i keep calling #id on the wrong object and i want it to throw an exception



19
20
21
# File 'lib/sup.rb', line 19

def id
  raise "wrong id called on #{self.inspect}"
end

#ignore_concurrent_calls(*methods) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/sup/util.rb', line 233

def ignore_concurrent_calls *methods
  methods.each do |meth|
    mutex = "@__concurrent_protector_#{meth}"
    flag = "@__concurrent_flag_#{meth}"
    oldmeth = "__unprotected_#{meth}"
    class_eval <<-EOF
      alias #{oldmeth} #{meth}
      def #{meth}(*a, &b)
        #{mutex} = Mutex.new unless defined? #{mutex}
        #{flag} = true unless defined? #{flag}
        run = #{mutex}.synchronize do
          if #{flag}
            #{flag} = false
            true
          end
        end
        if run
          ret = #{oldmeth}(*a, &b)
          #{mutex}.synchronize { #{flag} = true }
          ret
        end
      end
    EOF
  end
end

#returning(x) {|x| ... } ⇒ Object

“k combinator”

Yields:

  • (x)


213
# File 'lib/sup/util.rb', line 213

def returning x; yield x; x; end

#synchronized(*methods) ⇒ Object

clone of java-style whole-method synchronization assumes a @mutex variable TODO: clean up, try harder to avoid namespace collisions



222
223
224
225
226
227
228
229
230
231
# File 'lib/sup/util.rb', line 222

def synchronized *methods
  methods.each do |meth|
    class_eval <<-EOF
      alias unsynchronized_#{meth} #{meth}
      def #{meth}(*a, &b)
        @mutex.synchronize { unsynchronized_#{meth}(*a, &b) }
      end
    EOF
  end
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



216
# File 'lib/sup/util.rb', line 216

def tap; yield self; self; end