How to make instance variables private in Ruby? -


Is there a way to create example variables in "Ruby" private (C ++ or Java definition)? In other words, I want to follow the code as a result of an error.

class base def start () @ x = 10 end and received by class & lt; Base Def X @ X = 20 End and D = Detached.New

Most of the things Like Ruby, the example variables are not actually "private" and can be accessed by any person with d.instance_variable_get: @x .

Unlike Java / C ++, however, example variables are always private in Ruby, they are never part of the public API, as are the methods, because they are only those verbose Can be accessed with the guitar. So if there is any discretion in your API, then you do not have to worry about anyone who misuses your frequency variable, because they will use methods instead. (Of course, if someone wants to go wild and wants to use personal methods or example variables, there is no way to stop them.)

The only concern is that if someone accidentally < / Em> When they expand your class, then overwrites a frequency variable, it can probably be saved using names, possibly calling it @bas_x in its example.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -