# Unexpected behaviour using \`select\` with \`caller\`

**URL:** <https://forum.crystal-lang.org/t/unexpected-behaviour-using-select-with-caller/3027>\
**Category:** Help & Support\
**Created:** [March 15, 2021, 2:40am UTC](https://forum.crystal-lang.org/t/unexpected-behaviour-using-select-with-caller/3027 "2021-03-15T02:40:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![randy](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@randy](https://forum.crystal-lang.org/u/randy)\
**Post date:** [March 15, 2021, 2:40am UTC](https://forum.crystal-lang.org/t/unexpected-behaviour-using-select-with-caller/3027/1 "2021-03-15T02:40:03Z")

</div>

I was trying to write a method earlier today to grab all of the lines in the call stack matching the name of the file in which the method was defined, using `select` on the return value of `caller`, but it was always missing one line that I expected to be there.

To debug, I wrote the following code, derived from the method I was trying to write, into a file named `test.cr`

```auto
def a_method
  puts "Caller:"
  puts "\n"
  puts caller.join("\n")
  puts "\n\n"

  puts "Caller with select:"
  puts "\n"
  puts caller
    .select {|l| puts l; l.includes?("test")}
    .tap { puts "\n"} # just here to separate output of puts in `select` from output of expression
    .join("\n")
  puts "\n\n"

  puts "Caller with map then select:"
  puts "\n"
  puts caller
    .map(&.itself)
    .select {|l| puts l; l.includes?("test")}
    .tap { puts "\n"} # just here to separate output of puts in `select` from output of expression
    .join("\n")
end

a_method

```

The first chunk (“Caller”) just prints the output of `caller` so I know what it looks like. I would have expected the second and third chunks (“Caller with select” and “Caller with map then select”) to have the same output, because the .map(&.itself) would just return the same array it started out with.

Instead, the second chunk outputs only one line when I would have expected two. The third chunk gives me the expected output.

I put the `puts`es inside `select` to see what lines it was working with. To my surprise it was giving me  
`/usr/local/Cellar/crystal/0.36.1_3/src/enumerable.cr:1313:11 in 'a_method'`  
instead of something like  
`test.cr:ln:col in 'a_method'`  
which is what I got in the 3rd chunk, and it’s also what I was expecting.

Are my expectations off, or is this a bug?

This is the entire screen output when I run the method.

```auto
Caller:

test.cr:4:8 in 'a_method'
test.cr:24:1 in '__crystal_main'
/usr/local/Cellar/crystal/0.36.1_3/src/crystal/main.cr:110:5 in 'main_user_code'
/usr/local/Cellar/crystal/0.36.1_3/src/crystal/main.cr:96:7 in 'main'
/usr/local/Cellar/crystal/0.36.1_3/src/crystal/main.cr:119:3 in 'main'

Caller with select:

/usr/local/Cellar/crystal/0.36.1_3/src/enumerable.cr:1313:11 in 'a_method'
test.cr:24:1 in '__crystal_main'
/usr/local/Cellar/crystal/0.36.1_3/src/crystal/main.cr:110:5 in 'main_user_code'
/usr/local/Cellar/crystal/0.36.1_3/src/crystal/main.cr:96:7 in 'main'
/usr/local/Cellar/crystal/0.36.1_3/src/crystal/main.cr:119:3 in 'main'

test.cr:24:1 in '__crystal_main'

Caller with map then select:

test.cr:17:8 in 'a_method'
test.cr:24:1 in '__crystal_main'
/usr/local/Cellar/crystal/0.36.1_3/src/crystal/main.cr:110:5 in 'main_user_code'
/usr/local/Cellar/crystal/0.36.1_3/src/crystal/main.cr:96:7 in 'main'
/usr/local/Cellar/crystal/0.36.1_3/src/crystal/main.cr:119:3 in 'main'

test.cr:17:8 in 'a_method'
test.cr:24:1 in '__crystal_main'
```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [March 15, 2021, 12:59pm UTC](https://forum.crystal-lang.org/t/unexpected-behaviour-using-select-with-caller/3027/2 "2021-03-15T12:59:18Z")

</div>

Reduced reproduction:

```auto
def a_method
  p! caller.first # => "/eval:2:6 in 'a_method'"
  p! caller.select { true }.first # => "/usr/lib/crystal/enumerable.cr:1313:11 in 'a_method'"
  p! caller.map(&.itself).first # => "/eval:4:5 in 'a_method'"
end

a_method

```

For some reason the first call frame of `caller.select { true }` points to `enumerable.cr:1313`, that’s the first line of `#select`: [crystal/enumerable.cr at 0.36.1 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/blob/0.36.1/src/enumerable.cr#L1313)

---

<div class="post-metadata">

**Author:** ![randy](https://avatars.discourse-cdn.com/v4/letter/r/2acd7d/32.png) [@randy](https://forum.crystal-lang.org/u/randy)\
**Post date:** [March 18, 2021, 3:37am UTC](https://forum.crystal-lang.org/t/unexpected-behaviour-using-select-with-caller/3027/3 "2021-03-18T03:37:04Z")

</div>

Thanks for the reply. I’m gathering this is not the intended behaviour. Would you recommend submitting an issue?

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [March 18, 2021, 11:10am UTC](https://forum.crystal-lang.org/t/unexpected-behaviour-using-select-with-caller/3027/4 "2021-03-18T11:10:10Z")

</div>

Yes, this sure looks like a bug.
