Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you access an instance variable within a mixin method?

How do you access an instance variable within a mixin method? I can think of 2 ways, but both seem problematic.

  1. Have the mixin method access the instance variable directly as any class method would, e.g self.text. Problem with this is that it places restrictions on where the mixin method can be used, and forces the class doing the mixing to have a particular instance method named in a particular way.

  2. Pass the instance variable as a parameter to the mixin method, which would result in code like this:

example

self.do_something(self.text)

or

@thing.do_something(@thing.text)

which looks nasty to me, and doesn't conform to the principles of object orientation.

Is there any other way to do it?, am I right to be concerned?

like image 737
dangerousdave Avatar asked Feb 19 '10 09:02

dangerousdave


People also ask

How do you access instance variables in a method?

Instance variables can be accessed directly by calling the variable name inside the class. However, within static methods (when instance variables are given accessibility), they should be called using the fully qualified name. ObjectReference. VariableName.

Can we use instance variable in method?

No. 1. Variables declared within a method are local variables. An instance variable is declared inside a class but outside of any method or block.

What do you call methods used to access instance variables?

A class's methods can access the class's instance variables and call its methods. In particular, the main method for a class is often used to test the class's methods. An instance variable is accessed with the expression referenceVariable. instanceVariableName.

Can we access instance variable in main method in Java?

Yes. There§s no absolute rule that "you may not access any instance variable in a static method". The only rule is that to access an instance variable, you first need an instance whose instance variable you wish to access.


1 Answers

In general, avoid having mixins access member variables: It's a very tight form of coupling that can make future refactoring unnecessarily difficult.

One useful strategy is for the Mixin to always access variables via accessors. So, instead of:

#!/usr/bin/ruby1.8

module Mixin

  def do_something
    p @text
  end

end

class Foo

  include Mixin

  def initialize
    @text = 'foo'
  end

end

Foo.new.do_something     # => "foo"

the mixin accesses the "text" accessor, which is defined by the including class:

module Mixin

  def do_something
    p text
  end

end

class Foo

  attr_accessor :text

  include Mixin

  def initialize
    @text = 'foo'
  end

end

Foo.new.do_something     # => "foo"

What if you need to include the Mixin in this class?

class Foo

def initialize
  @text = "Text that has nothing to do with the mixin"
end

end

Using generic and common data names in mixins can lead to conflicts when the including class uses the same name. In that case, have the mixin look for data with a less common name:

module Mixin

  def do_something
    p mixin_text
  end

end

and let the including class define the appropriate accessor:

class Foo

  include Mixin

  def initialize
    @text = 'text that has nothing to do with the mixin'
    @something = 'text for the mixin'
  end

  def mixin_text
    @something
  end

end

Foo.new.do_something     # => "text for the mixin"

In this way, the accessor acts as sort of "impedance matcher" or "translator" between the mix-in's data and the including class's data.

like image 88
Wayne Conrad Avatar answered Sep 28 '22 02:09

Wayne Conrad