class Object
Public Instance Methods
axe(q, default=nil)
click to toggle source
# File lib/sup/util/axe.rb, line 4 def axe q, default=nil question = if default && !default.empty? "#{q} (enter for \"#{default}\"): " else "#{q}: " end ans = @cli.ask question ans.empty? ? default : ans.to_s end
axe_yes(q, default="n")
click to toggle source
# File lib/sup/util/axe.rb, line 14 def axe_yes q, default="n" axe(q, default) =~ /^y|yes$/i end
benchmark(s, &b)
click to toggle source
# File lib/sup/util.rb, line 233 def benchmark s, &b ret = nil times = Benchmark.measure { ret = b.call } debug "benchmark #{s}: #{times}" ret end
git_suffix()
click to toggle source
# File lib/sup/version.rb, line 1 def git_suffix revision = `GIT_DIR=#{__dir__}/../../.git git rev-parse HEAD 2>/dev/null` if revision.empty? "-git-unknown" else "-git-#{revision[0..7]}" end end
id()
click to toggle source
this is for debugging purposes because i keep calling id
on the wrong object and i want it to throw an exception
# File lib/sup.rb, line 19 def id raise "wrong id called on #{self.inspect}" end
ignore_concurrent_calls(*methods)
click to toggle source
# File lib/sup/util.rb, line 207 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;| ... }
click to toggle source
“k combinator”
# File lib/sup/util.rb, line 187 def returning x; yield x; x; end
synchronized(*methods)
click to toggle source
clone of java-style whole-method synchronization assumes a @mutex variable TODO: clean up, try harder to avoid namespace collisions
# File lib/sup/util.rb, line 196 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;| ... }
click to toggle source
# File lib/sup/util.rb, line 190 def tap; yield self; self; end