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
How to Set the Default String Encoding on Ruby 1.9
How to Convert PDF Files to Images Using Rmagick and Ruby
Is Inject the Same Thing as Reduce in Ruby
What Does the Fail Keyword Do in Ruby
How to Check If a Variable Is a Number or a String
I Have a Gem Installed But Require 'Gemname' Does Not Work. Why
What Does "Wrong Number of Arguments (1 for 0)" Mean in Ruby
Get Class Location from Class Object
Put Haml Tags Inside Link_To Helper
How to Get the Latest Record from Each Group in Activerecord
Sum 2 Hashes Attributes with the Same Key
Rails Link_To External Site, Url Is Attribute of User Table, Like: @Users.Website
How to Check Whether a Value in a String Is an Ip Address
How to Avoid Putting the Magic Encoding Comment on Top of Every Utf-8 File in Ruby 1.9
Cannot Load Such File -- Readline (Loaderror) When Running Heroku Create --Stack Cedar
Why Does Accessing a Ssl Site with Mechanize on Windows Fail, But on MAC Work