Class: Enumerator
- Defined in:
- lib/core/facets/to_hash.rb,
lib/core/facets/enumerator.rb,
lib/core/facets/enumerator/fx.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Yielder
Instance Method Summary collapse
-
#_start(*args, &receiver) ⇒ Object
private
:nodoc:.
- #fx ⇒ Object
- #fx_send(op, *a, &b) ⇒ Object private
-
#initialize(*args, &block) ⇒ Enumerator
constructor
private
Provides the ruby-1.9 block form of Enumerator, where you can write:.
- #old_initialize ⇒ Object private
-
#to_h(mode = nil) ⇒ Object
Convert an Enumerator object into a hash.
-
#to_h_assoc ⇒ Object
This is equivalent to Array#to_h_assoc.
-
#to_h_auto ⇒ Object
This is equivalent to Array#to_h_auto.
-
#to_h_flat ⇒ Object
This is equivalent to Array#to_h_flat.
-
#to_h_multi ⇒ Object
This is equivalent to Array#to_h_multi.
-
#to_h_splat ⇒ Object
This is equivalent to Array#to_h_splat.
Constructor Details
#initialize(*args, &block) ⇒ Enumerator (private)
Provides the ruby-1.9 block form of Enumerator, where you can write:
obj = Enumerator.new do |yielder|
# ...
yielder.yield(data) # or: yielder << data
# ...
end
When obj.each is called, the block is run once. It should call yielder.yield with each item it wishes to generate.
Example:
fib = Enumerator.new { |y|
a = b = 1
loop {
y << a
a, b = b, a + b
}
}
fib.take(10) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
37 38 39 40 41 42 43 44 |
# File 'lib/core/facets/enumerator.rb', line 37 def initialize(*args, &block) if block @body = block old_initialize(self, :_start) else old_initialize(*args) end end |
Instance Method Details
#_start(*args, &receiver) ⇒ Object (private)
:nodoc:
46 47 48 |
# File 'lib/core/facets/enumerator.rb', line 46 def _start(*args,&receiver) #:nodoc: @body.call(Yielder.new(&receiver), *args) end |
#fx ⇒ Object
15 16 17 |
# File 'lib/core/facets/enumerator/fx.rb', line 15 def fx Functor.new(&method(:fx_send).to_proc) end |
#fx_send(op, *a, &b) ⇒ Object (private)
22 23 24 |
# File 'lib/core/facets/enumerator/fx.rb', line 22 def fx_send(op, *a, &b) map{ |e| e.send(op, *a, &b) } end |
#old_initialize ⇒ Object (private)
12 |
# File 'lib/core/facets/enumerator.rb', line 12 alias :old_initialize :initialize |
#to_h(mode = nil) ⇒ Object
Convert an Enumerator object into a hash. This is equivalent to Array#to_h.
e1 = [[1,:a],[2,:b],[3,:c]].to_enum
e1.to_h #=> { 1=>:a, 2=>:b, 3=>:c }
e2 = [1,2,3,4,5].to_enum
e2.to_h #=> {5=>nil, 1=>2, 3=>4}
e3 = [1,2,1,3,1,5].to_enum
e3.to_h #=> {1=>5}
CREDIT: Sandor Szücs
289 290 291 |
# File 'lib/core/facets/to_hash.rb', line 289 def to_h(mode=nil) to_a.to_h(mode) end |
#to_h_assoc ⇒ Object
This is equivalent to Array#to_h_assoc.
313 314 315 |
# File 'lib/core/facets/to_hash.rb', line 313 def to_h_assoc to_a.to_h_assoc end |
#to_h_auto ⇒ Object
This is equivalent to Array#to_h_auto.
295 296 297 |
# File 'lib/core/facets/to_hash.rb', line 295 def to_h_auto to_a.to_h_auto end |
#to_h_flat ⇒ Object
This is equivalent to Array#to_h_flat.
307 308 309 |
# File 'lib/core/facets/to_hash.rb', line 307 def to_h_flat to_a.to_h_flat end |
#to_h_multi ⇒ Object
This is equivalent to Array#to_h_multi.
319 320 321 |
# File 'lib/core/facets/to_hash.rb', line 319 def to_h_multi to_a.to_h_multi end |
#to_h_splat ⇒ Object
This is equivalent to Array#to_h_splat.
301 302 303 |
# File 'lib/core/facets/to_hash.rb', line 301 def to_h_splat to_a.to_h_splat end |