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

#benchmark(s, &b) ⇒ Object



195
196
197
198
199
200
# File 'lib/sup/util.rb', line 195

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



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
# File 'lib/sup/util.rb', line 169

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

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

“k combinator”

Yields:

  • (x)


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

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



158
159
160
161
162
163
164
165
166
167
# File 'lib/sup/util.rb', line 158

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

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

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



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

def tap; yield self; self; end