It’s been a while since I last did Ruby programming — looking at somebody else’s code, I see the @ sigil in a function (not a method — external to any class definition), which I understood to be scoped to instance members.
Is the module the implied self in functions?
In a top-level function (still a method really), you’re in a slightly strange place: a global “main” space (see the prompt when you run
irb, or try looking atselfinside such a function), but also these functions are defined as private methods inside theObjectclass.You can sneak around this by explicitly declaring these methods
public, but I’m not sure I like this! Curiously, if you define a top-level method insideirb, then you can call it via the class methodObject#foowithout declaring it public.So this is a kind of an “implied main namespace hacked onto
Object(but shhh don’t tell anyone)”.@foodefined inside a top-level function is available inside Object, as a plain old attribute. Sort of. If your top-level method set@fooand you call it without scoping then it is declared in the eigen-likemainnamespace, but if you call the class method viaObjectthen@fooappears in the space ofObject.Another example:
gives
The converse also applies.