Class: Enumerator
Direct Known Subclasses
Class Method Summary collapse
-
.unfold(seed, &blk) ⇒ Enumerator
Creates an Enumerator that can unfold a sequence from a given seed.
Instance Method Summary collapse
-
#to_b ⇒ Boolean
Converts enum to a boolean.
Class Method Details
.unfold(seed, &blk) ⇒ Enumerator
Creates an Enumerator that can unfold a sequence from a given seed.
The block receives the current seed and should return a two-element
array [value, next_seed], or nil to end the sequence.
247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/mug/functional.rb', line 247 def unfold(seed, &blk) raise ArgumentError, 'no block given' unless block_given? Enumerator.new do |y| loop do result = blk.call(seed) break if result.nil? value, seed = result y << value end end end |
Instance Method Details
#to_b ⇒ Boolean
Converts enum to a boolean. Returns true if there are any elements. An enumerator whose size cannot be calculated lazily is assumed to be true.
149 150 151 |
# File 'lib/mug/bool.rb', line 149 def to_b (s = size).nil? || s.to_b end |