Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用默认库从其他继承中检查Ruby类所有者_Ruby_Class_Owner - Fatal编程技术网

使用默认库从其他继承中检查Ruby类所有者

使用默认库从其他继承中检查Ruby类所有者,ruby,class,owner,Ruby,Class,Owner,我想知道如何从其他类中检查某些方法/类的所有者 例如: 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 cla

我想知道如何从其他类中检查某些方法/类的所有者

例如:

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
类值
属性存取器:金钱
def初始化
@货币=0.0
终止
我能拿到钱吗
退钱
终止
def转账(目标、金额)
self.money-=金额
target.money+=金额
终止
终止
阶级国家
属性存取器:值
def初始化
@value=value.new
终止
终止
阶级国家
def初始化
超级的
终止
我要向这个国家致敬
价值。转移资金(国家价值,500)
终止
终止
国家级
def初始化
超级的
终止
我要向这个国家致敬
价值。转移资金(国家价值,200)
终止
终止
国家级
def初始化
超级的
终止
我要向这个国家致敬
价值。转账(国家价值,300)
终止
终止
是的,decentant如何循环是没有意义的,但是我想实现不同的子类有不同的参数的想法

这个列表相当长(我已经安装了其中至少40个,其中包括更复杂的desendant分支和更多从Value类调用transfer_money的方法)。然后我有一些想法来实现这个结构。我想添加currency,但覆盖所有transfer\u money方法调用对我来说将是一项艰巨的任务。因此,我创建了一个哈希表来为我生成调用

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
阶级国家
def self.get_后代
ObjectSpace.each_对象(类)。选择{| klass | klass
我需要弄清楚如何定义所有者类。我尝试使用调用者,但它返回字符串/字符串数组而不是对象,方法调用者仅适用于同一个方法,'sender'gem给了我一个想法,但它是用C编写的,由于我的情况,我需要使用默认库

非常感谢

编辑:

我将用较短的方式重写此问题:

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
从类
谁是我的所有者
返回self.class.owner,除非self.class.owner.nil?
终止
终止
课堂测试
属性存取器:测试
def初始化
@测试=Slave.new
终止
终止
类Test2测试
b、 testing.who_是_me#=>Test2的所有者
c、 谁是我的主人

这似乎是一个。。。奇怪的解决方案。你能用语言解释一下主要逻辑在做什么吗?这似乎是倒退和多余的。你给了我们一整段代码。相反,你只能给我们更具体的重点。主要的逻辑是在从不同继承初始化的类的方法内部实现新功能。这个新特性将检查方法所有者的所有者类,并为不同的所有者类返回不同的值。当Ruby有其他几种可能更好的方法时,为什么要颠覆OOP?我试图了解每种类型的实际逻辑/变化情况。我在这个问题上添加了更多信息,这可能会让我更清楚地了解我想要实现的目标。