2

I'm trying to get a better understanding of the instance variable usage outside of standard class declarations. This may be an odd example, but it's because the @ symbol is used outside of a class declaration and I have yet to see that.

require "mysql"

@db_host  = "localhost"
@db_user  = "root"
@db_pass  = "root"
@db_name = "your_db_name"

client = Mysql::Client.new(:host => @db_host, :username => @db_user, 
:password => @db_pass, :database => @db_name)
@cdr_result = client.query("SELECT * from your_db_table_name')
sawa
  • 165,429
  • 45
  • 277
  • 381

2 Answers2

1

In ruby, everything is a object. For instance, in your code you are actually in the main object.

In your file, if you do puts self.class, you will see you are under main object, and the class being Object.

In pratical terms, using @ or not, will take no difference. If you declare a class @myvar you will be within in the entire main object as a instance variable. Your variable will be visible in any method of that file.

def set_country
  @myvar = 'Brazil'
end

def get_country
  puts @myvar
  # @myvar is visible here
end

------------------

def set_country
  myvar = 'Brazil'
end

def get_country
  puts myvar
  # cannot access 'Brazil' value
end
Pedro Fernandes
  • 366
  • 3
  • 11
  • 1
    To add on, [here](https://stackoverflow.com/questions/917811/what-is-main-in-ruby) is more information on the `main` object. – pixatlazaki May 17 '18 at 23:38
  • 2
    It is often said that in Ruby, everything is an object. It is the first time I have seen anyone claiming that "[i]n [R]uby, everything is a class". – sawa May 18 '18 at 04:36
  • Besides the premise being incorrect, I don't see any logical connection between "everything is a class" and "you are ... in the main object", which you connected by the word "so". – sawa May 18 '18 at 04:37
  • "using @ or not in your example will have no difference" is plain wrong. – sawa May 18 '18 at 04:38
  • 1
    @sawa : Looking at this concrete example, I would indeed have said that there it doesn't make any difference whether or not to use a instance variable. Could you explain why you consider this statement wrong? – user1934428 May 18 '18 at 05:42
  • 1
    While I understand the premise of this answer; I also understand ruby. I am not certain you answer actually explains anything to someone who does not. I would highly suggest edditing your answer to be a bit more elaborate as well as a bit more informationally sound. – engineersmnky May 18 '18 at 18:14
  • I've changed my awnser. Now with an example showing the difference and fixing the english mistakes. – Pedro Fernandes May 21 '18 at 00:26
0

The initial execution context of a Ruby program is an object, that is an instance of the Object class. If you attempt to query the object with inspect or p, it will simply return "main" as is mentioned here

Setting values such as @blah will create instance variables on this top level object.

The following short snippet should demonstrate this.

Inspecting the context simply returns main, it's an Object, more specifically an instance of theObject class.

The initial list of instance variables is an empty array, but after you set the some using @blah, you can see they have been added to the list of instance variables.

In the example you posted, there is actually no need to use instance variables, local variables would have been fine.

p self
puts self.class
puts instance_of? Object
puts "instance variables = #{self.instance_variables}"
@db_host  = "localhost"
@db_user  = "root"
@db_pass  = "root"
@db_name = "your_db_name"
puts "instance variables = #{self.instance_variables}"
main 
Object
true
instance variables = []
instance variables = [:@db_host, :@db_user, :@db_pass, :@db_name]
nPn
  • 16,254
  • 9
  • 35
  • 58