# Very basic question - writing instance method

**URL:** <https://www.ruby-forum.com/t/very-basic-question-writing-instance-method/162894>\
**Category:** Rails\
**Created:** [March 15, 2009, 7:22am UTC](https://www.ruby-forum.com/t/very-basic-question-writing-instance-method/162894 "2009-03-15T07:22:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [March 15, 2009, 7:22am UTC](https://www.ruby-forum.com/t/very-basic-question-writing-instance-method/162894/1 "2009-03-15T07:22:55Z")

</div>

If we are calling an instance method on particular instance of an object  
as:  
@obj1.perform\_sanity\_check(@obj2)

Will the instance method perform\_sanity\_check have access to @obj1  
attributes?

How do I / Do I need to  
pass this @obj1 to perform\_sanity\_check?

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [March 15, 2009, 12:26pm UTC](https://www.ruby-forum.com/t/very-basic-question-writing-instance-method/162894/2 "2009-03-15T12:26:21Z")

</div>

On Mar 15, 6:22 am, Amita B. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> If we are calling an instance method on particular instance of an object  
> as:  
> @obj1.perform\_sanity\_check(@obj2)
> 
> Will the instance method perform\_sanity\_check have access to @obj1  
> attributes?

Yes.

Fred

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [March 15, 2009, 12:27pm UTC](https://www.ruby-forum.com/t/very-basic-question-writing-instance-method/162894/3 "2009-03-15T12:27:32Z")

</div>

> Will the instance method perform\_sanity\_check have access to @obj1  
> attributes?

yes, with  
self.attribute

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [March 16, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/very-basic-question-writing-instance-method/162894/4 "2009-03-16T15:01:49Z")

</div>

In object-oriented programming and your example, @obj1 is not being  
“passed” to perform\_sanity\_check, it is the “receiver object,” the  
object being “asked” to execute its perform\_sanity\_check method. In a  
way, you are sending a message to the object @obj, requesting that it  
execute one of its methods.

Does @obj1 have access to @obj1’s attributes? That depends on the  
context. If @obj1.perform\_sanity\_check appears in the implementation  
of one of @obj1’s instance methods, inside the @obj1’s class  
definition, then @obj1 can access the instance variables using  
self.instance\_variable or @instance\_variable. Otherwise, the instance  
variables aren’t directly accessible (Ruby encapsules object  
impelementation). They may be accessible through accessor methods,  
methods that may be defined in @obj1’s class definition provided to  
(indirectly) access them.

See: “The Ruby P.ming Language” by Flanagan & Matsumoto, Chapter  
7: Classes and Modules.

On Mar 14, 11:22 pm, Amita B. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![amita0204](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e56c9b/32.png) [@amita0204](https://www.ruby-forum.com/u/amita0204)\
**Post date:** [March 16, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/very-basic-question-writing-instance-method/162894/5 "2009-03-16T15:01:14Z")

</div>

In object-oriented programming and your example, @obj1 is not being  
“passed” to perform\_sanity\_check, it is the “receiver object,” the  
object being “asked” to execute its perform\_sanity\_check method. In a  
way, you are sending a message to the object @obj, requesting that it  
execute one of its methods.

Does @obj1 have access to @obj1’s attributes? That depends on the  
context. If @obj1.perform\_sanity\_check appears in the implementation  
of one of @obj1’s instance methods, inside the @obj1’s class  
definition, then @obj1 can access the instance variables using  
self.instance\_variable or @instance\_variable. Otherwise, the instance  
variables aren’t directly accessible (Ruby encapsules object  
impelementation). They may be accessible through accessor methods,  
methods that may be defined in @obj1’s class definition provided to  
(indirectly) access them.

See: “The Ruby P.ming Language” by Flanagan & Matsumoto, Chapter  
7: Classes and Modules.

On Mar 14, 11:22 pm, Amita B. \<rails-mailing-l…@andreas-
