0

In the following example everything is logical for me:

class Something
    def initialize
        @x=101
    end

    def getX
        return @x
    end
 end

obj = Something.new
puts obj.getX
=>101

Something.new will create new instance of class Something with instance variable @x which will be visible to all methods of class Something.

But what will happen in second example without initialize(constructor) method.

class Something
    def bla_bla_method
        @x=101
    end

    def getX
        return @x
    end
 end

obj = Something.new
puts obj.getX
=>nil
obj.bla_bla_method
puts obj.getX
=>101

So now bla_bla_method when called will create(like constructor) new instance_variable @x and will add that instance variable in "instance variable table" which is available to all methods again?

So now if i add new method "new_method" in class Something(in second example):

def new_method
    @x=201
end
...
obj.bla_bla_method
puts obj.getX
=>101

obj.new_method
puts obj.getX
=>201

So if im getting this right, every method of class can create new instance variable which is available to all methods of class? And then every method can overwrite that instance variable over and over again(cyclical)?

I'm new to ruby so maybe here i'm doing big mistake or don't understand some basic concept , so dont yell :D

8
  • 6
    You're correct. Commented Oct 22, 2013 at 18:26
  • 2
    Ha, exactly the minimum number of characters required for a comment. Efficient! Commented Oct 22, 2013 at 18:29
  • 1
    You have assessed the situation correctly. The most common use of instance variables is through "setters" and "getters", and other methods may change them as well. Commented Oct 22, 2013 at 18:32
  • 1
    Instance variables are available to the instance, obj in your examples. If you create another instance obj2 = Something.new it has its own set of variables. Commented Oct 22, 2013 at 18:33
  • @numbers1311407, Logan is no doubt a golfer. He probably wrote "You are correct.", then realized he could save a character and still meet the minimum length requirement by using an apostrophe. (An odd rule, incidentally, as one can always pad with gibberish.) Commented Oct 22, 2013 at 18:36

2 Answers 2

2

Instance variables for an object can be named and manipulated while the object exists. See the example below, when we are using the irb prompt object:

$ irb
> instance_variables    # => [:@prompt]
> @foo                  # => nil
> instance_variables    # => [:@prompt]
> @foo = 1              # => 1
> instance_variables    # => [:@prompt, :@foo]
> @foo                  # => 1

Now, here's a description of Class#new from the docs:

Calls allocate to create a new object of class’s class, then invokes that object’s initialize method, passing it args. This is the method that ends up getting called whenever an object is constructed using .new.

One way to think of this is that initialize is functionally a regular method just like your other instance methods, only it gets called by Class#new to provide us with an easy way of setting default values (among others).

Sign up to request clarification or add additional context in comments.

Comments

1

Technically, yes. But consider the notion of Object Oriented programming - Creating real world abstractions in form of classes and objects.

For instance, if you are talking about a student in a school; you know that is an abstractable entity. So you go ahead and encapsulate the common characteristic of student in a class Student.

initialize is a constructor. When you create a new student in your system, you would naturally want to supply few necessary details about him like his name, age and class. So in initialize method you set those instance variables.

Few students also study in school; so naturally they will acquire some grade and stuff; to instantiate those details about the student, you would want to do that with something like this:

#Student(name, age, class)
kiddorails = Student.new("Kiddorails", 7, 2)
#to grade:
kiddorails.set_grades
#do stuff

So you can mutate and set the instance variables in an object, almost anywhere you want in a class; but the point is - Do it, where it makes sense.

PS: You should always set default values to the instance variables which are not set via initialize in initialize, if needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.