Forum: Ruby on Rails very basic question - writing instance method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C7da1c726f89b6b7c37e8a22c0e0382d?d=identicon&s=25 Amita Bhatkhande (amita0204)
on 2009-03-15 07:22
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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-15 12:26
(Received via mailing list)
On Mar 15, 6:22 am, Amita Bhatkhande <rails-mailing-l...@andreas-
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
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-03-15 12:27
(Received via mailing list)
> Will the instance method perform_sanity_check have access to @obj1
> attributes?

yes, with
  self.attribute
988e49987971369d4903c02b3269c8b2?d=identicon&s=25 athem (Guest)
on 2009-03-16 15:01
(Received via mailing list)
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 Programming Language" by Flanagan & Matsumoto, Chapter
7: Classes and Modules.



On Mar 14, 11:22 pm, Amita Bhatkhande <rails-mailing-l...@andreas-
988e49987971369d4903c02b3269c8b2?d=identicon&s=25 athem (Guest)
on 2009-03-16 15:01
(Received via mailing list)
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 Programming Language" by Flanagan & Matsumoto, Chapter
7: Classes and Modules.



On Mar 14, 11:22 pm, Amita Bhatkhande <rails-mailing-l...@andreas-
This topic is locked and can not be replied to.