Learn Ruby
Instance Variable
21.1
Lesson

Is my coffee hot or cold

First let’s get a hot coffee.

In the above case coffee1 is served “hot”. Let’s add a method so that we could ask coffee if it is hot? The method name will be is_it_hot?. Let’s add this method.

We have defined method is_it_hot?. however there is a problem. Whether the coffee is hot or not this information is in method serve. In the method serve it is the condition variable that holds the information if the coffee is hot or not.

The method is_it_hot? can’t access condition variable because variable condition is in the method serve and only method serve has access to that variable.

In order to make variable condition available to other methods we need to turn that method into an instance variable.

In the next lesson let’s see one more example of it