What does it mean in the ruby language to create an instance variable outside of a class?
e.g:
def my_method:
@animal = "cat"
end
I've also seen it written outside of a method, like this:
@foo = "bar"
Is this just sugar syntax?
thanks!
What does it mean in the ruby language to create an instance variable outside of a class?
e.g:
def my_method:
@animal = "cat"
end
I've also seen it written outside of a method, like this:
@foo = "bar"
Is this just sugar syntax?
thanks!
what it means to create instance of variable outside class in ruby?
Instance variables have nothing to do with classes. They belong to objects, i.e. instances, that's why they are called "instance" variables.
def my_method @animal = "cat" end
This does not create an instance variable. It creates a method named my_method, which when you call it assigns to the instance variable named @animal.
There is nothing special about this. When you call this method, it will assign to the @animal instance variable of whatever object you call this method on, just like any other method that assigns an instance variable.
For example here:
class Foo
def my_method
@animal = 'cat'
end
end
foo = Foo.new
bar = Foo.new
If you call foo.my_method, it will assign to the instance variable @animal of foo, if you call bar.my_method, it will assign to the instance variable @animal of bar.
There is actually nothing different between those two examples as far as the instance variable is concerned.
The only interesting question is "Which class is the method defined in?" But that has nothing to do with instance variables.
Methods defined on the top-level become private instance methods of Object.
@foo = "bar"
Again, this is no different than any other assignment to an instance variable. This assigns to the instance variable named @foo of whatever object self is at the moment.
From an instance variable standpoint, there is absolutely nothing special about this. @foo = 'bar' always means "assign to the instance variable named @foo of whatever object self is at the moment". There is nothing else it can mean.
The only interesting question is: "What is self at this point?" But that has nothing to do with instance variables.
At the top-level, self is always the anonymous object commonly called main in the Ruby community.
is just sugar sintaxe?
No, this is not syntactic sugar for anything. It's just an assignment like any other assignment. There is absolutely nothing special about it.
If you do this on the command line:
% ruby -e "puts self; puts self.class"
main
Object
You'll see at the top-level scope (not inside a class or module), you'll be in the scope of an instance of Object called main.
What you already know about using instance variables and defining methods etc are the same for main as it is for class and module.
class Foo
end
foo_main = Foo.new # create instance of `Foo`
class Foo
self # then, inside scope of class `Foo`
# => Foo
def foo # define instance method for class `Foo`
puts "foo"
end
end
foo_main.foo # calls method on an instance of `Foo`
# => foo
# at the top level, it's as if the Ruby runtime has already done something
# like "Object.new" and then put you "inside" the created instance
my_main = self # inside scope of `main` already
# => main
def bar # define instance method for class `Object`
puts "bar"
end
# below are all equivalent
my_main.bar
self.bar
bar # implied `self` is main, an instance of `Object`
You may have noticed:
Foo, self returns Foo, a classself returns main, an instance of class ObjectAlthough it seems like they're two different things, what makes Ruby work the way it does is they're actually the same! More specifically, it's:
Foo, self returns Foo, an instance of class Classself returns main, an instance of class Object