Class: Array

Inherits:
Object show all
Defined in:
lib/rwd/rwd.rb,
lib/rwd/ruby.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.file(file) ⇒ Object



491
492
493
494
495
496
497
498
499
500
501
# File 'lib/rwd/ruby.rb', line 491

def self.file(file)
  res	= new

  File.open(file) do |f|
    f.readlines.uncomment.chomp.each do |line|
      res << line
    end
  end

  res
end

Instance Method Details

#anyObject



534
535
536
537
538
539
540
# File 'lib/rwd/ruby.rb', line 534

def any
  if empty?
    nil
  else
    self[rand(self.length)]
  end
end

#buildtreeObject



386
387
388
# File 'lib/rwd/ruby.rb', line 386

def buildtree
  self.dominoloop([self])
end

#chaosObject



521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/rwd/ruby.rb', line 521

def chaos
  res	= self.dup

  (length^2).times do
    a	= rand(length)
    b	= rand(length)

    res[a], res[b]	= res[b], res[a]
  end

  res
end

#chompObject



301
302
303
# File 'lib/rwd/ruby.rb', line 301

def chomp
  self.collect{|s| s.chomp}
end

#chomp!Object



297
298
299
# File 'lib/rwd/ruby.rb', line 297

def chomp!
  self.collect!{|s| s.chomp}
end

#compressObject



305
306
307
# File 'lib/rwd/ruby.rb', line 305

def compress
  self.collect{|s| s.compress}
end

#domino(tabellen, kolom = nil, onlymatchinglines = false) ⇒ Object



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/rwd/ruby.rb', line 340

def domino(tabellen, kolom=nil, onlymatchinglines=false)
  links	= self
  res		= []
  res		= self.dup	unless onlymatchinglines

  tabellen.each do |rechts|
    tmp	= []

    links.each do |l|
      if kolom.nil? or l.length == kolom
        rechts.each do |r|
          tmp << l + r[1..-1]	if l[-1] == r[0]
        end
      end
    end

    links	= tmp
    res.concat(tmp)
  end

  res	= res.sort.uniq
end

#dominoloop(tabellen) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/rwd/ruby.rb', line 363

def dominoloop(tabellen)
  lres	= []
  res		= self.dup
  kolom	= 2

  while lres.length != res.length do
    lres	= res.dup
    res	= res.domino(tabellen, kolom)

    res.each do |line|
      line << "*"	if (line.length != line.uniq.length and line[-1] != "*")
    end

    $stderr.print "#{100*(res.length)/(lres.length)}% "

    kolom += 1
  end

  $stderr.puts ""

  return res
end

#format(format) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/rwd/ruby.rb', line 445

def format(format)
  format	= format.gsub(/\s/, "")
  res		= []

  [format.length, self.length].min.times do |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

#idsObject



547
548
549
# File 'lib/rwd/ruby.rb', line 547

def ids
  collect{|e| e.ids}
end

#joinwords(sep = " ", quote = '"') ⇒ Object



333
334
335
336
337
338
# File 'lib/rwd/ruby.rb', line 333

def joinwords(sep=" ", quote='"')
  self.collect do |s|
    s	= quote + s + quote	if s =~ /[[:blank:]]/
    s
  end.join(sep)
end

#minmaxObject



542
543
544
545
# File 'lib/rwd/ruby.rb', line 542

def minmax
  min, value, max	= self
  [min, [value, max].min].max
end

#numsortObject



503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/rwd/ruby.rb', line 503

def numsort
  sort do |a, b|
    a2	= a.to_fs
    b2	= b.to_fs

    if a2.class != b2.class
      a2	= a
      b2	= b
    end

    a2 <=> b2
  end
end

#productObject



325
326
327
328
329
330
331
# File 'lib/rwd/ruby.rb', line 325

def product
  res	= 1
  self.each do |n|
    res *= n
  end
  res
end

#rotateObject



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/rwd/ruby.rb', line 551

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

#rwd_form(prefix, values = [], twoparts = 0, options = {}) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/rwd/rwd.rb', line 170

def rwd_form(prefix, values=[], twoparts=0, options={})
  res	= []

  res << "<table>"
  self.each_index do |n|
    name	= "#{prefix.to_html}#{self[n].downcase.to_html}"

    res << "<row>"
    res << "<p align='right'>"
    res << "#{self[n].to_html}:"
    res << "</p>"

    if options.keys.include?(self[n])
      res << "<select name='#{name}'>"
      res << options[self[n]].rwd_options(RWDEmptyline)
      res << "</select>"
    else
      s	= ""

      s << "<text name='#{name}'"
      s << " value='#{values[n].to_s.to_html}'"	if n < values.length
      s << "/>"

      res << s
    end

    res << "</row>"

    if twoparts > 0 and n == twoparts-1
      res << "<row><empty/></row>"
    end
  end
  res << "</table>"

  return res.join("\n")
end

#rwd_headers(emptyfield = false) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/rwd/rwd.rb', line 159

def rwd_headers(emptyfield=false)
  res	= ""

  res	= res + "<row>"
  res	= res + "<p/>"	if emptyfield
  res	= res + self.collect{|s| "<p align='left'><b>#{s.to_html}</b></p>" }.join("")
  res	= res + "</row>"

  return res
end

#rwd_method(method) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rwd/rwd.rb', line 120

def rwd_method(method)
  res	= ""

  self.each do |s|
    s		= s.join("/")	if s.kind_of?(Array)
    s2	= s.dup
    s2[0..0]	= s2[0..0].upcase
    res	= res + "<p align='left'><a action='#{method}/#{s.to_html}'>#{s2.to_html}</a></p>"
  end

  return res
end

#rwd_options(emptyline = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/rwd/rwd.rb', line 110

def rwd_options(emptyline=nil)
  if emptyline.nil?
    a	= self
  else
    a	= [emptyline].concat(self)
  end

  a.numsort.collect{|s| "<option>#{s.to_s.to_html}</option>" }.join("\n")
end

#rwd_row(key = nil, value = nil, bold = false) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'lib/rwd/rwd.rb', line 133

def rwd_row(key=nil, value=nil, bold=false)
  res	= ""

  res	= res + "<row valign='top'>"
  res	= res + "<radio name='#{key.to_html}' value='#{value.to_html}'/>"	unless key.nil?
  res	= res + self.collect{|s| "<p align='#{(s.kind_of?(Numeric) or s =~ /^\d+\.\d+$/) ? "right" : "left"}'>#{"<b>" if bold}#{s.to_s.to_html}#{"</b>" if bold}</p>"}.join("")
  res	= res + "</row>"

  return res
end

#rwd_table(headers = nil, highlightrows = []) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rwd/rwd.rb', line 144

def rwd_table(headers=nil, highlightrows=[])
  res	= ""

  highlightrows	= [highlightrows].flatten

  n	= -1

  res	= res + "<table>"
  res	= res + headers.rwd_row(nil, nil, true)	unless headers.nil?
  res	= res + self.collect{|a| a.rwd_row(nil, nil, highlightrows.include?(n+=1))}.join("")
  res	= res + "</table>"

  return res
end

#stripObject



313
314
315
# File 'lib/rwd/ruby.rb', line 313

def strip
  self.collect{|s| s.strip}
end

#subset(fields, values, results, exact = true, emptyline = nil, joinwith = nil) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/rwd/ruby.rb', line 390

def subset(fields, values, results, exact=true, emptyline=nil, joinwith=nil)
  fields	= [fields]		unless fields.kind_of? Array
  values	= [values]		unless values.kind_of? Array
  results	= [results]		unless results.kind_of? Array
  emptyline	= emptyline.downcase	unless emptyline.nil?
  res		= self.dup
  res.delete_if {true}

  self.each do |l|
    ok	= true

    case l.class.to_s
    when "String"
      c		= l.splitwords
      correction	= 1
      joinwith	= " "	if joinwith.nil?
    when "Array"
      c		= l
      correction	= 0
    end

    #catch :stop do
      values2	= values.dup
      fields.each do |f|
        v	= values2.shift
        v	= v.downcase	unless v.nil?
        if emptyline.nil? or (not v == emptyline)
          if exact
            unless (v.nil? or c[f-correction].downcase == v)
              ok	= false
              #throw :stop
            end
          else
            unless (v.nil? or c[f-correction].downcase.include?(v))
              ok	= false
              #throw :stop
            end
          end
        end
      end
    #end

    if ok
      res2	= []
      results.each do |n|
        res2 << c[n-1]
      end
      res2	= res2.join(joinwith)	unless joinwith.nil?
      res << res2
    end
  end

  return res
end

#sumObject



317
318
319
320
321
322
323
# File 'lib/rwd/ruby.rb', line 317

def sum
  res	= 0
  self.each do |n|
    res += n
  end
  res
end

#to_fsObject



517
518
519
# File 'lib/rwd/ruby.rb', line 517

def to_fs
  collect{|s| s.to_fs}
end

#to_hObject



569
570
571
572
573
574
575
576
577
578
579
# File 'lib/rwd/ruby.rb', line 569

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

#to_iObject



460
461
462
# File 'lib/rwd/ruby.rb', line 460

def to_i
  collect{|c| c.to_i}
end

#to_parObject



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/rwd/ruby.rb', line 464

def to_par
  dash	= self.dup
  alpha	= self.dup
  numeric	= self.dup

  dash.delete_if do |s|
    not (s =~ /\A-/) or
    (s =~ /\A-?[[:digit:]\.]+\z/) or
    (s =~ /^-+$/)
  end

  alpha.delete_if do |s|
    ((s =~ /\A-/) or
     (s =~ /\A-?[[:digit:]\.]+\z/)) and
    not ((s =~ /^\.+$/) or (s =~ /^-+$/))
  end

  numeric.delete_if do |s|
    not (s =~ /\A-?[[:digit:]\.]+\z/) or
    (s =~ /^\.+$/)
  end

  raise "Oops!"	if dash.length + alpha.length + numeric.length != length

  return dash, alpha, numeric
end

#uncommentObject



309
310
311
# File 'lib/rwd/ruby.rb', line 309

def uncomment
  self.join("\0").uncomment.split("\0")
end