0

Can someone explain this Ruby behavior? I can't find any mentions of this sequence in docs, perhaps I don't know where to look.

> var = "qw#@er"
 => "qw"
> var = 'qw#@er'
 => "qw\#@er"
> var = "qw#@@er"
NameError: uninitialized class variable @@er in Object
    from (irb):17:in `evaluate'
    from org/jruby/RubyKernel.java:1079:in `eval'
    from org/jruby/RubyKernel.java:1479:in `loop'
    from org/jruby/RubyKernel.java:1242:in `catch'
    from org/jruby/RubyKernel.java:1242:in `catch'
jva
  • 2,797
  • 1
  • 26
  • 41
  • http://www.railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/ – Steven Aguilar Jul 19 '19 at 15:07
  • 1
    `var = "qw#@er"` is the same as `var = "qw#{@er}"` since `@er` is `nil` it will generate only `"qw"` string – nuaky Jul 19 '19 at 15:13
  • 1
    I only found out about this quirk in the syntax recently, so don't feel bad, it's not very well documented. There's even been [debate about removing it](https://bugs.ruby-lang.org/issues/10541) because it can cause problems for people not expecting it. – tadman Jul 19 '19 at 20:15
  • To save page-turning: in short (from the selected answer at the link to the question for which this one is a dup), curly braces may be omitted when the reference is to a global, instance or class variable. (Didn't know that. Wish it weren't true.) – Cary Swoveland Jul 19 '19 at 23:03

0 Answers0