0

I make a simple Ruby class as follows in a Ruby script:

class Rock::Stone
end

When I run the script at the command prompt, I get:

`<main>': uninitialized constant Rock (NameError)

Why is this? I thought scope resolution operators were allowed in class defs?

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109

3 Answers3

1

I thought scope resolution operators were allowed in class defs?

They are allowed, but the containing module (or class) must exist first:

module Rock
end

class Rock::Stone
end

Rock::Stone.new
# => #<Rock::Stone:0x00000000a38248>
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
0

The encapsulating module/class must be declared first. Try this:

module Rock
  class Stone
  end
end
Linuxios
  • 34,849
  • 13
  • 91
  • 116
0

I think you might need to create a class or module Rock first, then Rock::Stone can exist.

This SO thread may help.

Community
  • 1
  • 1
andyg0808
  • 1,367
  • 8
  • 18