ev/ruby.rb in rwdtinker-1.67 vs ev/ruby.rb in rwdtinker-1.68
- old
+ new
@@ -16,18 +16,20 @@
tekens22 = tekens
tekens23 = tekens + "'"
tekens31 = '\w\s\r\n'
-RegExpStringWord = "([#{tekens11}]+)" ; RegExpWord = Regexp.new(RegExpStringWord)
-RegExpStringWord2 = "([#{tekens21}]([#{tekens22}]*[#{tekens23}])?)" ; RegExpWord2 = Regexp.new(RegExpStringWord2)
-RegExpStringText = "([#{tekens31}]+)" ; RegExpText = Regexp.new(RegExpStringText)
-RegExpStringFile = '(\w[\w\.\-]*)' ; RegExpFile = Regexp.new(RegExpStringFile)
-RegExpStringEmail = '([\w\-\.]+@[\w\-\.]+)' ; RegExpEmail = Regexp.new(RegExpStringEmail)
-RegExpStringURL = '(\w+:\/\/[\w\.\-]+(:\d*)?\/[\w\.\-\/\#\?\=\%]*)' ; RegExpURL = Regexp.new(RegExpStringURL)
-RegExpStringPrint = '([\w \t\r\n\`\~\!\@\#\$\%\^\&\*\(\)\-\+\=\[\]\{\}\;\:\'\"\,\.\/\<\>\?\\\|]+)' ; RegExpPrint = Regexp.new(RegExpStringPrint)
-RegExpStringDiff = '(^[\-\+]([^\-\+].*)?)' ; RegExpDiff = Regexp.new(RegExpStringDiff)
+RegExpStringWord = "([#{tekens11}]+)" ; RegExpWord = Regexp.new(RegExpStringWord)
+RegExpStringWord2 = "([#{tekens21}]([#{tekens22}]*[#{tekens23}])?)" ; RegExpWord2 = Regexp.new(RegExpStringWord2)
+RegExpStringText = "([#{tekens31}]+)" ; RegExpText = Regexp.new(RegExpStringText)
+RegExpStringFile = '(\w[\w\.\-]*)' ; RegExpFile = Regexp.new(RegExpStringFile)
+RegExpStringEmail = '([\w\-\.]+@[\w\-\.]+)' ; RegExpEmail = Regexp.new(RegExpStringEmail)
+RegExpStringURL = '(\w+:\/\/[\w\.\-]+(:\d*)?\/[\w\.\-\/\#\?\=\%]*)' ; RegExpURL = Regexp.new(RegExpStringURL)
+RegExpStringPrint = '([\w \t\r\n\`\~\!\@\#\$\%\^\&\*\(\)\-\+\=\[\]\{\}\;\:\'\"\,\.\/\<\>\?\\\|]+)' ; RegExpPrint = Regexp.new(RegExpStringPrint)
+RegExpStringDiff = '(^[\-\+]([^\-\+].*)?)' ; RegExpDiff = Regexp.new(RegExpStringDiff)
+RegExpStringHTHLink = '(`[\w\,]*\ba\b[^`]*`)' ; RegExpHTHLink = Regexp.new(RegExpStringHTHLink)
+RegExpStringHTHSpecial = '(`[^`]*`)' ; RegExpHTHSpecial = Regexp.new(RegExpStringHTHSpecial)
module Enumerable
def deep_dup
Marshal::load(Marshal::dump(dup))
end
@@ -62,10 +64,14 @@
class Numeric
def to_fs
to_f
end
+
+ def to_html(eolconversion=true)
+ self.to_s.to_html(eolconversion)
+ end
end
class Integer
def oct
n = self
@@ -209,12 +215,12 @@
return res
end
def splitwords(tokens=[])
- tokens = [tokens] unless tokens.kind_of?(Array)
- res = []
+ tokens = [tokens] unless tokens.kind_of?(Array)
+ res = []
self.splitblocks(["'", "'"], ['"', '"']).each do |type, s|
case type
when 0
tokens.each do |token|
@@ -435,17 +441,18 @@
return res
end
def format(format)
+ format = format.gsub(/\s/, "")
res = []
[format.length, self.length].min.times do |n|
- case format[n].chr
- when "i" then res << self[n].to_i
- when "s" then res << self[n].to_s
- when "x" then res << self[n]
+ case format[n].chr.downcase
+ when "i" then res << self[n].to_i
+ when "s" then res << self[n].to_s
+ else res << self[n]
end
end
res
end
@@ -538,10 +545,40 @@
end
def ids
collect{|e| e.ids}
end
+
+ def rotate
+ raise "Array has to be 2D (An Array of Arrays)." unless self.dup.delete_if{|a| a.kind_of?(Array)}.empty?
+
+ res = []
+
+ self[0].length.times do |x|
+ a = []
+
+ self.length.times do |y|
+ a << self[y][x]
+ end
+
+ res << a
+ end
+
+ res
+ end
+
+ def to_h
+ raise "Array has to be 2D (An Array of Arrays)." unless self.dup.delete_if{|a| a.kind_of?(Array)}.empty?
+
+ res = {}
+
+ self.each do |k, v, *rest|
+ res[k] = v
+ end
+
+ res
+ end
end
class Hash
def save(file, append=false)
org = {}
@@ -697,13 +734,17 @@
every(1) {} if windows?
end
def linux?
- not windows? and not cygwin?
+ not (target_os.downcase =~ /linux/).nil?
end
+def darwin?
+ not (target_os.downcase =~ /darwin/).nil?
+end
+
def windows?
not (target_os.downcase =~ /32/).nil?
end
def cygwin?
@@ -725,12 +766,13 @@
def temp
(ENV["TMPDIR"] or ENV["TMP"] or ENV["TEMP"] or "/tmp").gsub(/\\/, "/")
end
def stdtmp
- $stderr = $stdout = File.new("#{temp}/ruby.#{Process.pid}.log", "a") unless ARGV.include?("--rwd-exit")
+ $stderr = $stdout = File.new("#{temp}/ruby.#{Process.pid}.log", "a")
end
+stdtmp if defined?(RUBYSCRIPT2EXE) and (RUBYSCRIPT2EXE =~ /rubyw/i)
$nobm = false
def nobm
$nobm = true
@@ -743,11 +785,11 @@
else
return nil
end
end
- label = label.to_s
+ label = label.inspect #unless label.kind_of?(String)
res = nil
$bm_mutex = ($bm_mutex or Mutex.new)
$bm_mutex.synchronize do
@@ -755,33 +797,45 @@
require "ev/bm"
$bm = {}
at_exit do
- format1 = "%10s %10s %10s %10s %10s %10s %10s"
- format2 = "%10s %10.6f %10.6f %10.6f %10.6f %10.6f %10d"
+ l = $bm.keys.collect{|s| s.length}.max
+ #format1 = "%10s %10s %10s %10s %10s %10s %s"
+ #format2 = "%10.6f %10.6f %10.6f %10.6f %10.6f %10d %s"
+ #$stderr.puts format1 % ["USERCPU", "SYSCPU", "CUSERCPU", "CSYSCPU", "ELAPSED", "COUNT", "LABEL"]
+ #$bm.sort{|a, b| [b[1], b[0]] <=> [a[1], a[0]]}.each do |k, v|
+ #$stderr.puts format2 % (v + [k])
+ #end
- $stderr.puts format1 % ["LABEL", "USERCPU", "SYSCPU", "CUSERCPU", "CSYSCPU", "ELAPSED", "TIMES"]
- $bm.sort{|a, b| [a[1], a[0]] <=> [b[1], b[0]]}.each do |k, v|
- $stderr.puts format2 % [k, *v]
+ format1 = "%10s %10s %10s %s"
+ format2 = "%10.6f %10.6f %10d %s"
+ $bm.each do |k, v|
+ $bm[k] = [v[0]+v[1], v[4], v[5]]
+ end
+ $stderr.puts format1 % ["CPU", "ELAPSED", "COUNT", "LABEL"]
+ $bm.sort{|a, b| [b[1], b[0]] <=> [a[1], a[0]]}.each do |k, v|
+ $stderr.puts format2 % (v + [k])
end
end
end
$bm[label] = [0.0]*5 + [0] unless $bm.include?(label)
+ $bm[label][5] += 1
end
if block_given?
bm = Benchmark.measure{res = yield}
bma = bm.to_a # [dummy label, user CPU time, system CPU time, childrens user CPU time, childrens system CPU time, elapsed real time]
+ $bm_last = bma
+
$bm_mutex.synchronize do
+ e = $bm[label]
0.upto(4) do |n|
- $bm[label][n] += bma[n+1]
+ e[n] += bma[n+1]
end
-
- $bm[label][5] += 1
end
end
res
end
@@ -814,6 +868,22 @@
res = block.call(*args)
hash[args] = res
end
res
end
+end
+
+def ask(options, text=false)
+ i = 0
+ $stderr.puts ""
+ options.each do |s|
+ $stderr.puts " %d %s" % [i+=1, s]
+ end
+ $stderr.puts ""
+ $stderr.print "? "
+ res = $stdin.gets
+ unless res.nil?
+ res = res.strip
+ res = options[res.to_i-1] if text and not res.empty?
+ end
+ res
end