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

#1

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

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

848caa92-07da-4e55-82b9-1f358de91bf4
UUID.random
UUID.random
#2

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, 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.


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

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

This way when you access TEST2 you have UUID.random

1 Like
#3

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!