0

I wondering of how to check the owner of certain method/class from other class.

For example:

class Value
  attr_accessor :money
  def initialize
    @money = 0.0
  end
  def get_money
    return self.money
  end
  def transfer_money(target, amount)
    self.money -= amount
    target.money += amount
  end
end

class Nation
  attr_accessor :value
  def initialize
    @value = Value.new 
  end
end

class Nation_A < Nation
  def initialize
    super
  end
  def pay_tribute_to_decendant_country
    value.transfer_money(Nation_B.value, 500)
  end
end

class Nation_B < Nation
  def initialize
    super
  end
  def pay_tribute_to_decendant_country
    value.transfer_money(Nation_C.value, 200)
  end
end

class Nation_C < Nation
  def initialize
    super
  end
  def pay_tribute_to_decendant_country
    value.transfer_money(Nation_A.value, 300)
  end
end

Yea, makes no sense how the decendant goes in a circle, but I'd like to implement the idea that different subclass has different argument.

The list is pretty long (I've installed at least 40 of these already with much more complex desendant branches and much more methods that call transfer_money from Value class). Then I have some idea to implement to the structure. I'd like to add currency, but to override all transfer_money method call would be a tremendous task for me to apply. Therefore I create a hash table that generate the call for me.

class Nation
  def self.get_descendants
    ObjectSpace.each_object(Class).select { |klass| klass < self }
  end
end

module Additional_Value

  currency_table = {}

  min = 50
  max = 100

  def self.range (min, max)
    rand * (max-min) + min
  end

  Nation.get_descendants.each do |derived_classes|
      currency_table[derived_classes] == self.range min, max
  end

end

class Value
  attr_accessor :currency
  def initialize
    @money = 0
    @currency = Additional_Value::currency_table
  end
  def transfer_money(target, amount)
    self.money -= amount
    amount = amount * @currency[self.class.owner] / @currency[target.class.owner]
    target.money += amount
  end
end

and I need to figure out how to define owner class. I tried using the caller, but it returns me string / array of string instead of object, method or calle work only for the same method, the 'sender' gem gives me an idea, but it's written in C, and I need to use the default library due to my circumstances.

Greatly appreciated.

Edit:

I'll rewrite the problem in a shorter way:

class Slave
  def who_is_the_owner_of_me
    return self.class.owner unless self.class.owner.nil?
  end
end

class Test
  attr_accessor :testing
  def initialize
    @testing = Slave.new
  end
end

class Test2 < Test1
end

a = Test.new
b = Test2.new
c = Slave.new
a.testing.who_is_the_owner_of_me  #=> Test
b.testing.who_is_the_owner_of_me  #=> Test2
c.who_is_the_owner_of_me          #=> main
user3163916
  • 318
  • 4
  • 16
  • 1
    This seems like an... odd solution. Can you explain in words what the main logic is doing? It seems all backwards and redundant. – Dave Newton Jan 16 '14 at 22:15
  • 1
    You gave a full chunk of code to us..rather you can give us only the focused point to be more specific.. – Arup Rakshit Jan 16 '14 at 22:15
  • The main logic is to implement new feature inside of a method of a class that is initialized from different inheritance. This new feature would check the owner class of the method owner, and return different values for different owner class. – user3163916 Jan 16 '14 at 22:18
  • 1
    That seems like a terrible way to do something like this. Why do you want to subvert OOP when Ruby has several other ways that are likely better? I'm trying to understand the actual logic/what's changing for each type. – Dave Newton Jan 16 '14 at 22:27
  • I've added more information on the question, it might give a clearer vision of what I would like to achieve. – user3163916 Jan 16 '14 at 22:33
  • I think what he means is "figure out what class's instance variable was used to access the object that's executing a given method." Which…yikes. – Chuck Jan 16 '14 at 22:33
  • Yes, Chuck, thank you for clarifying. My programming vocab is pretty bad. There's a gem called sender, but it's written in C, I'm wondering if the same process could be done in ruby. – user3163916 Jan 16 '14 at 22:36

0 Answers0