# Does Iterator work on all methods that take blocks?

**URL:** <https://forum.crystal-lang.org/t/does-iterator-work-on-all-methods-that-take-blocks/4322>\
**Category:** Help & Support\
**Created:** [January 31, 2022, 7:47pm UTC](https://forum.crystal-lang.org/t/does-iterator-work-on-all-methods-that-take-blocks/4322 "2022-01-31T19:47:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [January 31, 2022, 7:47pm UTC](https://forum.crystal-lang.org/t/does-iterator-work-on-all-methods-that-take-blocks/4322/1 "2022-01-31T19:47:22Z")

</div>

[Iterator(T) - Crystal 1.3.2](https://crystal-lang.org/api/1.3.2/Iterator.html) mentions an Iterator working on Range#each if used without a block, does that trick similarly for any method that requires a block?

---

<div class="post-metadata">

**Author:** ![RespiteSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/respitesage/32/793_2.png) [@RespiteSage](https://forum.crystal-lang.org/u/RespiteSage)\
**Post date:** [January 31, 2022, 7:50pm UTC](https://forum.crystal-lang.org/t/does-iterator-work-on-all-methods-that-take-blocks/4322/2 "2022-01-31T19:50:26Z")

</div>

Are you asking if `SomeClass.each` will always return an `Iterator` if you don’t give it a block? If so, the answer is no. `#each` methods that return `Iterator` are specifically written for each type that implements it.

---

<div class="post-metadata">

**Author:** ![RespiteSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/respitesage/32/793_2.png) [@RespiteSage](https://forum.crystal-lang.org/u/RespiteSage)\
**Post date:** [January 31, 2022, 8:25pm UTC](https://forum.crystal-lang.org/t/does-iterator-work-on-all-methods-that-take-blocks/4322/3 "2022-01-31T20:25:30Z")

</div>

Just to follow up on this, I wanted to show a fairly typical example:

`Range#each`

```crystal
  def each
    {% if B == Nil %}
      {% raise "Can't each beginless range" %}
    {% end %}

    if @begin.nil?
      raise ArgumentError.new("Can't each beginless range")
    end

    ItemIterator.new(self)
  end

```

([source](https://github.com/crystal-lang/crystal/blob/1.3.2/src/range.cr#L144))

This example shows that we can’t just have a block-less `#each` return an `Iterator` because implementing types (almost) always require their own custom iterators. In the case of `Range`, it’s `ItemIterator`, which is [here](https://github.com/crystal-lang/crystal/blob/1.3.2/src/range.cr#L461), if you’re interested. If you want to learn more, especially if you’re implementing your own `Iterator`, I highly recommend looking through [the module source](https://github.com/crystal-lang/crystal/blob/1.3.2/src/iterator.cr) to see the implementations of iterators there.

Note: all of the code and links here are for Crystal 1.3.2.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rogerdpack/32/117_2.png) [@rogerdpack](https://forum.crystal-lang.org/u/rogerdpack)\
**Post date:** [January 31, 2022, 9:44pm UTC](https://forum.crystal-lang.org/t/does-iterator-work-on-all-methods-that-take-blocks/4322/4 "2022-01-31T21:44:48Z")

</div>

Thanks!

As a point of feedback to the core people, it seems surprising to me that this code:

```auto
(1..10_000_000).select(&.even?)

```

behaves far differently than the iterator version:

```auto
(1..10_000_000).each.select(&.even?)

```

If it were me I’d try to make it more clear somehow, to readers…maybe add the word lazy to the name?

```auto
(1..10_000_000).lazy_each.select(&.even?)

```

To make it clearer? Even though that’s less ruby-like. Cheers!
