class Foo
def initialize
bar = 10
end
fiz = 5
end
Is there a possibility to get these local values (outside the class) ?
class Foo
def initialize
bar = 10
end
fiz = 5
end
Is there a possibility to get these local values (outside the class) ?
No. Once a local variable goes out of scope (for bar that is when the initialize
method has run - for fiz
when the end
of the class definition has been reached), it's gone. No trace left.
While a local variable is still in scope you can see it (well, its name) with local_variables
and get and set its value with eval
(though that's definitely not recommended for sanity reasons), but once it's out of scope, that's it. No way to get it back.
The local variable in initialize would be lost.
You are able to get the value fiz outside of the class, but only upon defining that class, and recording the return of the definition of the class.
return_of_class_definition = (class A ; fiz = 5 ; end)
would assign the value of fiz to the variable.