dkmd_nielsen
10/15/2008 3:07:00 PM
On Oct 15, 9:56 am, Jesús Gabriel y Galán <jgabrielyga...@gmail.com>
wrote:
> On Wed, Oct 15, 2008 at 4:44 PM, dkmd_nielsen <d...@cmscms.com> 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.
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.