`puts $0` raise error

# 1.cr
puts $0
 ╰─ $ cr run 1.cr
Unhandled exception: Nil assertion failed (NilAssertionError)
  from /home/zw963/Crystal/share/crystal/src/nil.cr:108:5 in 'not_nil!'
  from /home/zw963/Crystal/share/crystal/src/crystal/once.cr:44 in '__crystal_main'
  from /home/zw963/Crystal/share/crystal/src/crystal/main.cr:115:5 in 'main_user_code'
  from /home/zw963/Crystal/share/crystal/src/crystal/main.cr:101:7 in 'main'
  from /home/zw963/Crystal/share/crystal/src/crystal/main.cr:127:3 in 'main'
  from /usr/lib/libc.so.6 in '??'
  from /usr/lib/libc.so.6 in '__libc_start_main'
  from ../sysdeps/x86_64/start.S:117 in '_start'
  from ???

i know i made a mistake, what i want is, PROGRAM_NAME, it same as $0 in Ruby.

But, above error really surprising, why backtrace missing the puts $0 line?

There’s a long-standing issue about “globals” in general: Nil assertion failed when accessing $~ · Issue #4776 · crystal-lang/crystal · GitHub

Some background on $0: $0 needs to go · Issue #4715 · crystal-lang/crystal · GitHub (the meaning of PROGRAM_NAME has been removed. It’s now equivalent to $~.not_nil![0], in the same way as all other $ variables)

1 Like

Sorry, what does this means?

I consider, $0 now is equivalent to $~.not_nil![0], right? (equals to Ruby’s $&)

Yes, sorry. That’s a typo. It’s meant to be “now”. Fixed it in the original comment…