- Timestamp:
- Jan 21, 2018, 12:10:09 AM (6 years ago)
- Location:
- EcnlProtoTool/trunk/mruby-1.3.0
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
EcnlProtoTool/trunk/mruby-1.3.0/mrbgems/mruby-enum-ext/mrblib/enum.rb
r321 r331 59 59 def take(n) 60 60 raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int) 61 raise ArgumentError, "attempt to take negative size" if n < 0 62 63 n = n.to_int 64 ary = [] 65 self.each do |*val| 66 break if ary.size >= n 61 i = n.to_int 62 raise ArgumentError, "attempt to take negative size" if i < 0 63 ary = [] 64 return ary if i == 0 65 self.each do |*val| 67 66 ary << val.__svalue 67 i -= 1 68 break if i == 0 68 69 end 69 70 ary … … 124 125 block.call(ary.dup) if ary.size == n 125 126 end 127 nil 126 128 end 127 129 … … 154 156 end 155 157 block.call(ary) unless ary.empty? 158 nil 156 159 end 157 160 … … 215 218 # If the enumerable is empty, the first form returns <code>nil</code>, and the 216 219 # second form returns an empty array. 217 def first(n=NONE) 218 if n == NONE 220 def first(*args) 221 case args.length 222 when 0 219 223 self.each do |*val| 220 224 return val.__svalue 221 225 end 222 226 return nil 227 when 1 228 n = args[0] 229 raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int) 230 i = n.to_int 231 raise ArgumentError, "attempt to take negative size" if i < 0 232 ary = [] 233 return ary if i == 0 234 self.each do |*val| 235 ary << val.__svalue 236 i -= 1 237 break if i == 0 238 end 239 ary 223 240 else 224 a = [] 225 i = 0 226 self.each do |*val| 227 break if n<=i 228 a.push val.__svalue 229 i += 1 230 end 231 a 241 raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 0..1)" 232 242 end 233 243 end … … 385 395 first = false 386 396 else 397 val = val.__svalue 387 398 if block 388 max = val .__svalue if block.call(*val, max) > 0389 min = val .__svalue if block.call(*val, min) < 0399 max = val if block.call(val, max) > 0 400 min = val if block.call(val, min) < 0 390 401 else 391 val = val.__svalue392 402 max = val if (val <=> max) > 0 393 403 min = val if (val <=> min) < 0 … … 574 584 # 575 585 576 def cycle(n=nil, &block) 577 return to_enum(:cycle, n) if !block && n.nil? 578 579 ary = [] 580 if n.nil? 581 self.each do|*val| 582 ary.push val 583 block.call(*val) 584 end 585 loop do 586 ary.each do|e| 587 block.call(*e) 588 end 589 end 586 def cycle(nv = nil, &block) 587 return to_enum(:cycle, nv) unless block 588 589 n = nil 590 591 if nv.nil? 592 n = -1 590 593 else 591 raise TypeError, "no implicit conversion of #{n.class} into Integer" unless n.respond_to?(:to_int) 592 593 n = n.to_int 594 self.each do|*val| 595 ary.push val 596 end 597 count = 0 598 while count < n 599 ary.each do|e| 600 block.call(*e) 601 end 602 count += 1 603 end 604 end 594 unless nv.respond_to?(:to_int) 595 raise TypeError, "no implicit conversion of #{nv.class} into Integer" 596 end 597 n = nv.to_int 598 unless n.kind_of?(Integer) 599 raise TypeError, "no implicit conversion of #{nv.class} into Integer" 600 end 601 return nil if n <= 0 602 end 603 604 ary = [] 605 each do |*i| 606 ary.push(i) 607 yield(*i) 608 end 609 return nil if ary.empty? 610 611 while n < 0 || 0 < (n -= 1) 612 ary.each do |i| 613 yield(*i) 614 end 615 end 616 617 nil 605 618 end 606 619
Note:
See TracChangeset
for help on using the changeset viewer.