WPeN2Ic850EU
WPeN2Ic850EU

Reputation: 995

Direct reading of an instance variable

For some reason this code will work:

class Foo
  @foo = "foo"
end

Foo.new.@foo # => "foo"

And I see, this feature has been used in standard library specs, but it seems to be undocumented one. So, the question is, what is the status of the feature, should one use it?

Upvotes: 3

Views: 84

Answers (1)

Johannes Müller
Johannes Müller

Reputation: 5661

This feature is currently used for internal purposes, but it is discussed to limit external access to instance variable. A behaviour similar to protected methods might be useful.

The issue for this is crystal-lang/crystal#6066.

I would generally avoid this feature, as it is undocumented and very likely to be changed at some point.

Upvotes: 2

Related Questions