InQusitive InQusitive - 1 year ago 83
Ruby Question

What is a self reference in ruby?

Currently I am reading

The Ruby Programming Language
and it has mentioning of
in many places and I am not able to understand it's exact use. Is its behaviour similar to
pointer in C++.What all difference those two have?(I am not asking about the pointer dereferencing or any C++ specific things, in general) And when do we use
in our ruby code?

Answer Source

self is a variable that points to the object whose scope the current code is in. You would use self.something when calling a class method on that object.
For instance, if you had the following method:

class Foo
       puts "Class method!"

You would call the bar method by calling

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download