Break OO privacy: Difference between revisions
extended ruby example (instance_variable_get)
m (Added Sidef) |
(extended ruby example (instance_variable_get)) |
||
Line 962:
=={{header|Ruby}}==
Ruby lets you redefine great parts of the object model at runtime and provides several methods to do so conveniently. For a list of all available methods look up the documentation of <code>Object</code> and <code>Module</code> or call informative methods at runtime (<code>puts Object.methods</code>).
<lang ruby>
class Example
>> private▼
@private_data = "nothing" # instance variables are always private
>> "secret"▼
>> end▼
def hidden_method
>> example = Example.new▼
end
NoMethodError: private method `name' called for #<Example:0x101308408>▼
p example.private_methods(false) # => [:hidden_method]
▲#p example.hidden_method # => NoMethodError: private method `name' called for #<Example:0x101308408>
p example.instance_variables # => [:@private_data]
p example.instance_variable_get :@private_data # => "nothing"
p example.instance_variable_set :@private_data, 42 # => 42
p example.instance_variable_get :@private_data # => 42
</lang>
=={{header|Scala}}==
|