# Lame question about instance variable and attribute

**URL:** <https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988>\
**Category:** Ruby\
**Created:** [October 15, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988 "2008-10-15T16:45:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [October 15, 2008, 4:45pm UTC](https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988/1 "2008-10-15T16:45:48Z")

</div>

Assuming an attribute :field is defined, is there a processing  
difference between using @field and self.field [within the defining  
class?] Does the ruby interpretter take a different path in yielding  
the value depending how it is referenced? I haven’t notice anything.  
I’m just curious if the interpretter takes extra steps when yielding  
self.field.

Thanks

---

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [October 15, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988/2 "2008-10-15T16:56:57Z")

</div>

On Wed, Oct 15, 2008 at 4:44 PM, dkmd\_nielsen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Assuming an attribute :field is defined, is there a processing  
> difference between using @field and self.field [within the defining  
> class?] Does the ruby interpretter take a different path in yielding  
> the value depending how it is referenced? I haven’t notice anything.  
> I’m just curious if the interpretter takes extra steps when yielding  
> self.field.

@field accessed the instance variable directly, while self.field calls  
the method “field” on self. So the paths are completely different:

irb(main):001:0\> require ‘parse\_tree’  
=\> true  
irb(main):027:0\> class B  
irb(main):028:1\> attr\_reader :b  
irb(main):029:1\> def test  
irb(main):030:2\> @b  
irb(main):031:2\> end  
irb(main):035:1\> def test3  
irb(main):036:2\> self.b  
irb(main):037:2\> end  
irb(main):038:1\> end  
=\> nil  
irb(main):039:0\> ParseTree.new.parse\_tree\_for\_method(B, :test)  
=\> [:defn, :test, [:scope, [:block, [:args], [:ivar, :@b]]]]  
irb(main):040:0\> ParseTree.new.parse\_tree\_for\_method(B, :test3)  
=\> [:defn, :test3, [:scope, [:block, [:args], [:call, [:self], :b]]]]

Jesus.

---

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [October 15, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988/3 "2008-10-15T17:10:28Z")

</div>

On Oct 15, 9:56 am, Jesús Gabriel y Galán [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> irb(main):038:1\> end  
> =\> nil  
> irb(main):039:0\> ParseTree.new.parse\_tree\_for\_method(B, :test)  
> =\> [:defn, :test, [:scope, [:block, [:args], [:ivar, :@b]]]]  
> irb(main):040:0\> ParseTree.new.parse\_tree\_for\_method(B, :test3)  
> =\> [:defn, :test3, [:scope, [:block, [:args], [:call, [:self], :b]]]]
> 
> Jesus.

So, in processing that involves many iterations, one should most  
definitely use the instance variable reference. Correct?

When would it be proper to use the self. reference?

Thanks, again.

---

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [October 15, 2008, 5:26pm UTC](https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988/4 "2008-10-15T17:26:22Z")

</div>

dkmd\_nielsen wrote:

> When would it be proper to use the self. reference?

I think never. The exception is when the setter or getter is nontrivial  
(not defined by attr\_reader and others), but with attr\_accessors there’s  
no use calling the setters and getters from within the same class,  
accessing the variables directly is better.

TPR.

---

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [October 15, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988/5 "2008-10-15T17:45:47Z")

</div>

On Oct 15, 7:41 am, dkmd\_nielsen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Assuming an attribute :field is defined, is there a processing  
> difference between using @field and self.field [within the defining  
> class?] Does the ruby interpretter take a different path in yielding  
> the value depending how it is referenced? I haven’t notice anything.  
> I’m just curious if the interpretter takes extra steps when yielding  
> self.field.
> 
> Thanks

In terms of speed, it’s very close, but using @field is slightly  
faster, at least on my system.

Rehearsal -----------------------------------------------  
using self: 4.640000 2.350000 6.990000 ( 7.032596)  
using @: 4.550000 2.350000 6.900000 ( 6.928132)  
------------------------------------- total: 13.890000sec

```
              user system total real

```

using self: 4.660000 2.380000 7.040000 ( 7.137048)  
using @: 4.570000 2.360000 6.930000 ( 7.075875)

Benchmark code:  
require ‘benchmark’

class Dummy  
attr\_reader :foo  
def initialize  
@foo = 7983.3435  
end

def using\_self  
self.foo  
end

def using\_direct  
@foo  
end  
end

n = 1000000  
@d = Dummy.new  
Benchmark.bmbm(7) do |x|  
x.report(“using self:”) { n.times do @d.using\_self; end }  
x.report(“using @:”) { n.times do @d.using\_direct; end }  
end

---

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [October 15, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988/6 "2008-10-15T22:25:29Z")

</div>

On Oct 15, 10:40 am, Bryan D [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> using @: 4.570000 2.360000 6.930000 ( 7.075875)  
> def using\_self  
> Benchmark.bmbm(7) do |x|  
> x.report(“using self:”) { n.times do @d.using\_self; end }  
> x.report(“using @:”) { n.times do @d.using\_direct; end }  
> end

Thanks for the commentary, guys. This is the kind of stuff that is  
useful to me. Most everything I do i very iterative. Fortunately, if  
you can call it fortunate, most of my bottlenecks have nothing to do  
with Ruby. Most of the time is I/O related stuff between servers.

Thanks again,  
Happy Halloween

---

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [October 15, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988/7 "2008-10-15T23:01:27Z")

</div>

dkmd\_nielsen wrote:

> Assuming an attribute :field is defined, is there a processing  
> difference between using @field and self.field [within the defining  
> class?]

Remember that you don’t have to write “self.field” - “field” by itself  
is fine, as long as you haven’t assigned to a local variable called  
“field” within the calling method.

def field  
123  
end

puts field # 123  
field = 999  
puts field # 999  
puts field() # 123  
puts self.field # 123

---

<div class="post-metadata">

**Author:** ![dkmd\_nielsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/958977/32.png) [@dkmd\_nielsen](https://www.ruby-forum.com/u/dkmd_nielsen)\
**Post date:** [October 15, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/lame-question-about-instance-variable-and-attribute/150988/8 "2008-10-15T17:18:07Z")

</div>

Hi –

On Thu, 16 Oct 2008, dkmd\_nielsen wrote:

> > @field accessed the instance variable directly, while self.field calls  
> > irb(main):036:2\> self.b  
> > So, in processing that involves many iterations, one should most  
> > definitely use the instance variable reference. Correct?
> 
> When would it be proper to use the self. reference?

The self. version is necessary if you do, or think you’ll ever do, any  
kind of override. In other words, if you’ve got:

def name  
@name.upcase  
end

or whatever, then you’ll want to use the method, unless you’re doing  
something even more low-level (like assigning directly to @name).

David
