# Calling a method on string causes it to work differently in macros

**URL:** <https://forum.crystal-lang.org/t/calling-a-method-on-string-causes-it-to-work-differently-in-macros/693>\
**Category:** Help & Support\
**Created:** [April 23, 2019, 6:39am UTC](https://forum.crystal-lang.org/t/calling-a-method-on-string-causes-it-to-work-differently-in-macros/693 "2019-04-23T06:39:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [April 23, 2019, 6:39am UTC](https://forum.crystal-lang.org/t/calling-a-method-on-string-causes-it-to-work-differently-in-macros/693/1 "2019-04-23T06:39:34Z")

</div>

Does anyone know why TEST1 works just fine, but the second I call a method on the string it no longer works.

```auto
require "uuid"

TEST1 = "UUID.random"
TEST2 = "UUID.random123"[0..10]
TEST3 = "UUID!random".gsub("!", ".")
    
MACRO_TEST1 = {{TEST1.id}}
MACRO_TEST2 = {{TEST2.id}}
MACRO_TEST3 = {{TEST3.id}}
    
puts MACRO_TEST1
puts MACRO_TEST2
puts MACRO_TEST3
```

Outputs

```auto
848caa92-07da-4e55-82b9-1f358de91bf4
UUID.random
UUID.random
```

---

<div class="post-metadata">

**Author:** ![bew](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bew/32/113_2.png) [@bew](https://forum.crystal-lang.org/u/bew)\
**Post date:** [April 23, 2019, 11:09am UTC](https://forum.crystal-lang.org/t/calling-a-method-on-string-causes-it-to-work-differently-in-macros/693/2 "2019-04-23T11:09:07Z")

</div>

When you have `TEST2 = "UUID.random123"[0..10]`, using `TEST2` in macros will get you exactly `"UUID.random123"[0..10]` (not `UUID.random`).

Have a look at this: [https://carc.in/#/r/6s1e](https://carc.in/#/r/6s1e), we can inspect the source code assigned to `TEST`.

You can see what is exactly happening with your code using `{% debug %}` in `begin/end` macro block: [https://carc.in/#/r/6s1h](https://carc.in/#/r/6s1h).

* * *

You probably want to do this instead to do the changes in macro-land:

```auto
TEST1 = "UUID.random"
TEST2 = {{ "UUID.random123"[0..10] }}
TEST3 = {{ "UUID!random".gsub(/!/, ".") }}

```

This way when you access `TEST2` you have `UUID.random`

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [April 23, 2019, 4:14pm UTC](https://forum.crystal-lang.org/t/calling-a-method-on-string-causes-it-to-work-differently-in-macros/693/3 "2019-04-23T16:14:28Z")

</div>

Oh ok! I was wondering why debug wasn’t working, I guess I need to have it in a block of some kind (apparently `if true` also works). Thank you!
