Class: Array
- Defined in:
- lib/linguistics/en.rb,
lib/rwd/rwd.rb,
lib/rwd/ruby.rb,
lib/linkparser/utils.rb
Overview
Some minor modifications to the base Array class.
Class Method Summary collapse
Instance Method Summary collapse
-
#add!(other) ⇒ Object
Performs ‘+’ in place.
-
#all_fail?(test, *args) ⇒ Boolean
Returns whether or not the array has no elements which respond positively to the provided test.
-
#all_pass?(test, *args) ⇒ Boolean
Returns whether or not the array has no elements which respond negatively to the provided test.
- #any ⇒ Object
-
#any_fail?(test, *args) ⇒ Boolean
Returns whether or not the array has any elements which respond negatively to the provided test.
-
#any_pass?(test, *args) ⇒ Boolean
Returns whether or not the array has any elements which respond positively to the provided test.
- #buildtree ⇒ Object
- #chaos ⇒ Object
- #chomp ⇒ Object
- #chomp! ⇒ Object
-
#combine(other) ⇒ Object
Returns every possible combination of the elements in both this array and the supplied other array.
-
#combine!(other) ⇒ Object
Performs combine in place.
- #compress ⇒ Object
- #domino(tabellen, kolom = nil, onlymatchinglines = false) ⇒ Object
- #dominoloop(tabellen) ⇒ Object
- #format(format) ⇒ Object
- #ids ⇒ Object
- #joinwords(sep = " ", quote = '"') ⇒ Object
-
#left ⇒ Object
Returns all ‘left’ elements.
- #minmax ⇒ Object
-
#minus(other) ⇒ Object
Like “-”, but uses == instead of an id comparison.
-
#minus!(other) ⇒ Object
Like minus, but in place.
- #numsort ⇒ Object
- #product ⇒ Object
-
#right ⇒ Object
Returns all ‘right’ elements.
- #rotate ⇒ Object
- #rwd_form(prefix, values = [], twoparts = 0, options = {}) ⇒ Object
- #rwd_headers(emptyfield = false) ⇒ Object
- #rwd_method(method) ⇒ Object
- #rwd_options(emptyline = nil) ⇒ Object
- #rwd_row(key = nil, value = nil, bold = false) ⇒ Object
- #rwd_table(headers = nil, highlightrows = []) ⇒ Object
-
#separate(value = :__no_arg__, &block) ⇒ Object
Returns a new Array that has had a new member inserted between all of the current ones.
-
#separate!(value = :__no_arg__) ⇒ Object
The same as #separate, but modifies the Array in place.
-
#set_combine(other) ⇒ Object
Creates the set of all the combinations of the members of this set and another.
-
#set_combine!(other) ⇒ Object
Performs set_combine in place.
-
#set_equal?(other) ⇒ Boolean
Performs a varient on ‘==’, similar to what would be set equality.
-
#set_include?(other) ⇒ Boolean
Performs a varient on ‘include?’, only assuming that the elements of both arrays are sets of objects.
-
#set_of_sets_equal?(other) ⇒ Boolean
Performs a varient on ‘==’, similar to what would be set equality, but with the extra assumption that the elements of this set are sets themselves.
-
#set_of_sets_minus(other) ⇒ Object
Performs a varient on ‘-’, similar to what would be set subtraction, but with the extra assumption that the elements of this set are sets themselves.
-
#set_of_sets_minus!(other) ⇒ Object
Performs set_of_sets_minus in place.
- #strip ⇒ Object
- #subset(fields, values, results, exact = true, emptyline = nil, joinwith = nil) ⇒ Object
- #sum ⇒ Object
-
#superimpose(other) ⇒ Object
Superimposes one array over another, overwriting non-nil values.
-
#superimpose!(other) ⇒ Object
Performs superimpose in place.
- #to_fs ⇒ Object
- #to_h ⇒ Object
- #to_i ⇒ Object
- #to_par ⇒ Object
- #uncomment ⇒ Object
Class Method Details
Instance Method Details
#add!(other) ⇒ Object
Performs ‘+’ in place.
67 68 69 |
# File 'lib/linkparser/utils.rb', line 67 def add!( other ) self.replace(self + other) # not optimized. end |
#all_fail?(test, *args) ⇒ Boolean
Returns whether or not the array has no elements which respond positively to the provided test.
178 179 180 |
# File 'lib/linkparser/utils.rb', line 178 def all_fail?(test, *args) self.find_all {|elem| elem.send(test, *args)}.empty? end |
#all_pass?(test, *args) ⇒ Boolean
Returns whether or not the array has no elements which respond negatively to the provided test.
172 173 174 |
# File 'lib/linkparser/utils.rb', line 172 def all_pass?(test, *args) self.find_all {|elem| ! elem.send(test, *args)}.empty? end |
#any ⇒ Object
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 |
#any_fail?(test, *args) ⇒ Boolean
Returns whether or not the array has any elements which respond negatively to the provided test.
166 167 168 |
# File 'lib/linkparser/utils.rb', line 166 def any_fail?(test, *args) self.find {|elem| ! elem.send(test, *args)} end |
#any_pass?(test, *args) ⇒ Boolean
Returns whether or not the array has any elements which respond positively to the provided test.
160 161 162 |
# File 'lib/linkparser/utils.rb', line 160 def any_pass?(test, *args) self.find {|elem| elem.send(test, *args)} end |
#buildtree ⇒ Object
386 387 388 |
# File 'lib/rwd/ruby.rb', line 386 def buildtree self.dominoloop([self]) end |
#chaos ⇒ Object
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 |
#chomp ⇒ Object
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 |
#combine(other) ⇒ Object
Returns every possible combination of the elements in both this array and the supplied other array. Combinations are formed using the ‘+’ method.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/linkparser/utils.rb', line 54 def combine(other) return other.dup if self.empty? return self.dup if other.empty? temp = [] self.each {|mine| other.each {|theirs| temp << mine + theirs } } temp end |
#combine!(other) ⇒ Object
Performs combine in place
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/linkparser/utils.rb', line 39 def combine!(other) return self.replace(other) if self.empty? return self if other.empty? temp = self.dup self.clear temp.each {|mine| other.each {|theirs| self << mine + theirs } } self end |
#compress ⇒ Object
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 |
#ids ⇒ Object
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 |
#left ⇒ Object
Returns all ‘left’ elements.
34 35 36 |
# File 'lib/linkparser/utils.rb', line 34 def left self.find_all {|elem| elem.left?} end |
#minmax ⇒ Object
542 543 544 545 |
# File 'lib/rwd/ruby.rb', line 542 def minmax min, value, max = self [min, [value, max].min].max end |
#minus(other) ⇒ Object
Like “-”, but uses == instead of an id comparison.
109 110 111 |
# File 'lib/linkparser/utils.rb', line 109 def minus(other) self.dup.minus!(other) end |
#minus!(other) ⇒ Object
Like minus, but in place.
114 115 116 117 118 119 |
# File 'lib/linkparser/utils.rb', line 114 def minus!(other) other.each {|bad| self.delete_if {|elem| elem == bad} } self end |
#numsort ⇒ Object
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 |
#product ⇒ Object
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 |
#right ⇒ Object
Returns all ‘right’ elements.
29 30 31 |
# File 'lib/linkparser/utils.rb', line 29 def right self.find_all {|elem| elem.right?} end |
#rotate ⇒ Object
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, ={}) 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 .keys.include?(self[n]) res << "<select name='#{name}'>" res << [self[n]].(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 (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 |
#separate(value = :__no_arg__, &block) ⇒ Object
Returns a new Array that has had a new member inserted between all of the current ones. The value used is the given value
argument unless a block is given, in which case the block is called once for each pair of the Array, and the return value is used as the separator.
1579 1580 1581 1582 1583 |
# File 'lib/linguistics/en.rb', line 1579 def separate( value=:__no_arg__, &block ) ary = self.dup ary.separate!( value, &block ) return ary end |
#separate!(value = :__no_arg__) ⇒ Object
The same as #separate, but modifies the Array in place.
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 |
# File 'lib/linguistics/en.rb', line 1586 def separate!( value=:__no_arg__ ) raise ArgumentError, "wrong number of arguments: (0 for 1)" if value == :__no_arg__ && !block_given? (1..( (self.length * 2) - 2 )).step(2) do |i| if block_given? self.insert( i, yield(self[i-1,2]) ) else self.insert( i, value ) end end self end |
#set_combine(other) ⇒ Object
Creates the set of all the combinations of the members of this set and another.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/linkparser/utils.rb', line 137 def set_combine(other) if self.empty? return other elsif other.empty? return self else results = [] self.each {|a| other.each {|b| results << a.combine(b) } } return results end end |
#set_combine!(other) ⇒ Object
Performs set_combine in place.
154 155 156 |
# File 'lib/linkparser/utils.rb', line 154 def set_combine!(other) self.replace(self.set_combine(other)) end |
#set_equal?(other) ⇒ Boolean
Performs a varient on ‘==’, similar to what would be set equality.
104 105 106 |
# File 'lib/linkparser/utils.rb', line 104 def set_equal?(other) (self.minus other).empty? and (other.minus self).empty? end |
#set_include?(other) ⇒ Boolean
Performs a varient on ‘include?’, only assuming that the elements of both arrays are sets of objects.
97 98 99 100 101 |
# File 'lib/linkparser/utils.rb', line 97 def set_include?(other) self.find {|set| set.set_equal?(other) } end |
#set_of_sets_equal?(other) ⇒ Boolean
Performs a varient on ‘==’, similar to what would be set equality, but with the extra assumption that the elements of this set are sets themselves.
77 78 79 |
# File 'lib/linkparser/utils.rb', line 77 def set_of_sets_equal?(other) self.set_of_sets_minus(other).empty? and other.set_of_sets_minus(self).empty? end |
#set_of_sets_minus(other) ⇒ Object
Performs a varient on ‘-’, similar to what would be set subtraction, but with the extra assumption that the elements of this set are sets themselves.
84 85 86 |
# File 'lib/linkparser/utils.rb', line 84 def set_of_sets_minus(other) self.dup.set_of_sets_minus!(other) end |
#set_of_sets_minus!(other) ⇒ Object
Performs set_of_sets_minus in place.
89 90 91 92 93 |
# File 'lib/linkparser/utils.rb', line 89 def set_of_sets_minus!(other) self.delete_if {|set| other.set_include?(set) } end |
#strip ⇒ Object
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 |
#sum ⇒ Object
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 |
#superimpose(other) ⇒ Object
Superimposes one array over another, overwriting non-nil values.
122 123 124 |
# File 'lib/linkparser/utils.rb', line 122 def superimpose(other) self.dup.superimpose!(other) end |
#superimpose!(other) ⇒ Object
Performs superimpose in place.
127 128 129 130 131 132 133 |
# File 'lib/linkparser/utils.rb', line 127 def superimpose!(other) return self unless other.respond_to?(:[]) other.each_with_index {|o,i| (self[i] = o) unless(self[i]) } self end |
#to_fs ⇒ Object
517 518 519 |
# File 'lib/rwd/ruby.rb', line 517 def to_fs collect{|s| s.to_fs} end |
#to_h ⇒ Object
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_i ⇒ Object
460 461 462 |
# File 'lib/rwd/ruby.rb', line 460 def to_i collect{|c| c.to_i} end |
#to_par ⇒ Object
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 |
#uncomment ⇒ Object
309 310 311 |
# File 'lib/rwd/ruby.rb', line 309 def uncomment self.join("\0").uncomment.split("\0") end |