-1

Is it possible to have a class method that prints the name of the variable its parent class is assigned to? Example:

class Test
  def who_am_I_assigned_to
    puts "#{??}"
  end
end

THIS = Test.new
THIS.who_am_I_assigned_to
# >> THIS
sawa
  • 165,429
  • 45
  • 277
  • 381
QOps1981
  • 27
  • 1
  • 6
  • 1
    this might help https://stackoverflow.com/questions/2603617/ruby-print-the-variable-name-and-then-its-value but I can't think of a good use case for this, why do you need this behaviour ? – Subash Mar 06 '19 at 22:46
  • 2
    No. Consider: `THAT = cat = dog = OWL = THIS = Test.new`. What is the name of the constant or variable to which `Test.new` is assigned? This is not limited to instances; it applies to all Ruby objects. – Cary Swoveland Mar 06 '19 at 22:50
  • The reason I was looking for this behavior was to use the variable name as a name and also as a position within a grid of data. So if had a 2 dimensional Grid of variable names like "AA", "AB", "BC", etc. And it could know its name it would also know its position. There are a lot of ways I can get the position in the class, But I was curious if there was an interesting Rubyism I just didn't know about yet. – QOps1981 Mar 06 '19 at 22:57
  • Your example does not match your text. `THIS` is not a variable, it is a constant. – sawa Mar 07 '19 at 06:53
  • Furthermore, `THIS` is an instance of `Test`. It is not a class. `who_am_I_assinged_to` is not a class method. – sawa Mar 07 '19 at 07:46

1 Answers1

0

Technically yes, sort of.

But you'd have to recursively search through all the instance_variables of every object. And, as Cary points out, an object can be assigned to more than one variable at a time.

Don't do that.

The reason I was looking for this behavior was to use the variable name but also as a position within a grid of data. So if had a 2 dimensional Grid the variable names would be like "AA", "AB", "BC", etc. It is knew its name it would also know its position.

What you're describing is an "action at a distance" anti-pattern where changes to one part of a program change another part of a program with no obvious connection. This violates encapsulation; good encapsulation allows you to understand a single piece of a system by just looking at its inputs and outputs. Violating encapsulation means in order to understand one part of the system you have to understand every part of the system leading to a maintenance nightmare. Modern languages and practices strive to avoid this as much as possible.

For example, a variable's name should never matter to the behavior of the program. You should be able to safely perform a Rename Variable refactoring to name a variable according to what makes sense to the user of the object. In your example this would alter the behavior of the program violating the Principle of Least Astonishment.

Instead you'd have an object to represent your Grid and this would manage the relationships between Nodes in the Grid. Rather than passing around individual Nodes you'd pass around the Grid.

Or each Node can know who their neighboring Nodes are. An example of this would be a traditional Tree, Graph, or Linked List structure. The advantage here is there is no fixed position and the data structure can grow or shrink in any direction. Any Node can be passed around and it knows its position within the structure.

Schwern
  • 153,029
  • 25
  • 195
  • 336