Skip to content

Instantly share code, notes, and snippets.

@alinpopa
Created May 31, 2011 20:11
Show Gist options
  • Save alinpopa/1001177 to your computer and use it in GitHub Desktop.
Save alinpopa/1001177 to your computer and use it in GitHub Desktop.
Ruby instance_eval
require 'thread'
class Context
class << self
def evaluate(&block)
self.new.instance_eval(&block)
end
end
def self.execute(&block)
self.new.instance_eval(&block)
end
def initialize
@mutex = Mutex.new
end
def sync(&block)
@mutex.synchronize{
yield
}
end
def par(no_of_threads=1,&block)
threads = []
no_of_threads.times{
threads << Thread.new{
yield
}
}
threads.each{|t| t.join}
end
end
Context.execute {
par(10) {
sync {
puts "test"
}
}
}
Context.evaluate {
par(10) {
sync {
puts "test"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment