Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we are using _ prefix in some cases to define instance variables in ruby?

Tags:

oop

ruby

I see in ruby classes some people using _ prefix to define instance variables like @_property while in normal condition attr_writer uses the normal name like @property to define instance variables.

What is the point of doing this and what is the difference?

like image 479
Moeen Avatar asked Jan 25 '23 04:01

Moeen


1 Answers

The _ prefix isn't a language feature, it doesn't do anything special, it is just a convention.

In most cases, the developer wants to indicate that a variable is not in use, for example, a block variable that is not used in a block. Or they want to indicate that the variable is an internal variable of a module or gem and that others should not read or modify this variable directly.

like image 197
spickermann Avatar answered Jan 26 '23 17:01

spickermann