Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 666
WPeN2Ic850EU Avatar asked Mar 15 '26 16:03

WPeN2Ic850EU


1 Answers

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.

like image 198
Johannes Müller Avatar answered Mar 18 '26 04:03

Johannes Müller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!