Access Instance Variable from Outside the Class

Access instance variable from outside the class

Yes, you can use instance_variable_get like this:

class Hello
def method1
@hello = "pavan"
end
end

h = Hello.new
p h.instance_variable_get(:@hello) #nil
p h.method1 #"pavan" - initialization of @hello
p h.instance_variable_get(:@hello) #"pavan"

If the variable is undefined (first call of instance_variable_get in my example) you get nil.


As Andrew mention in his comment:

You should not make this the default way you access instance variables as it violates encapsulation.

A better way is to define an accessor:

class Hello
def method1
@hello = "pavan"
end
attr_reader :hello
end

h = Hello.new
p h.hello #nil
p h.method1 #"pavan" - initialization of @hello
p h.hello #"pavan"

If you want another method name, you could alias the accessor: alias :my_hello :hello.

And if the class is not defined in your code, but in a gem: You can modify classes in your code and insert new functions to classes.

Unable to access instance variable outside the class in ruby

In order to print an instance variable, you must access it with the context of the instance.

p @obj.stacks

Accessing class variables outside the class in python

First you need to create an instance of the class, then you can use that instance to access its instance variable and function.

Below is a simple example based on your code

import tkinter as tk

class First:
def __init__(self, root):
self.root = root
self.root.title('First window')
self.root.geometry('1350x700+0+0')

self.mystring = tk.StringVar(self.root)

self.txt_id = tk.Entry(self.root, textvariable=self.mystring, font=("Times New Roman", 14), bg='white')
self.txt_id.place(x=200, y=400, width=280)

btn_search = tk.Button(self.root, text="Show in class", command=self.get_id)
btn_search.place(x=100, y=150, width=220, height=35)

def get_id(self):
val = self.mystring.get()
print("inside class:", val)
return val

root = tk.Tk()

# create an instance of First class
app = First(root)

def show():
# get the text from the Entry widget
print("outside class:", app.get_id())

tk.Button(root, text="Show outside class", command=show).pack()

root.mainloop()

Note that I have change class name from first to First because it is normal practise.

Accessing instance variable in rails and ruby

Use attr_reader to read the value of an instance variable

class Person
attr_reader :name

def initialize(name)
@name = name
end
end

john = Person.new("John")
john.name #=> "John"

attr_reader adds a getter method to the class, in this case

def name
@name
end

Hope that helps!

How to access variable from outside class in another file

Those are not global variables. They are called "instance variables" and to access them you need to create instances of your casinos and players. Looks like this.

player = Player.new
player.money # => 0
player.money += 10
player.money # => 10

In your Casino class you don't call parent initializers (a simple oversight, I think), so it doesn't initialize @name and @money.

And Roulette doesn't do anything at all to obtain a wallet. So it stays at default value nil.



Related Topics



Leave a reply



Submit