如何在ruby中创建类实例变量

时间:2015-09-17 22:50:50

标签: ruby class methods

我有以下代码:

def self.test
  # clear out all the other products so we have a clean slate
  BF.delete_all_products

  BF.puts_and_file("\n\n## ==== scenario #{@scenario_timestamp} =====\n\n")
  BF.puts_and_file "endpoint: #{@baseUrl}"

  @aggregating_PRP = BF.create_aggregating_rate_plan

  pro_plan_id = BF.create_product(@create_pro_20_5_5)['id']
  biz_plan_id = BF.create_product(@create_biz)['id']

  @pro_20_5_plan = BF.create_rate_plan(pro_plan_id, @create_rate_plan_for_pro_20_5_5)
  @biz_plan = BF.create_rate_plan(biz_plan_id, @create_rate_plan_for_biz_monthly)
end

我想通过这样做来替换所有@instance变量赋值:

def self.create_products
  @aggregating_PRP = BF.create_aggregating_rate_plan

  pro_plan_id = BF.create_product(@create_pro_20_5_5)['id']
  biz_plan_id = BF.create_product(@create_biz)['id']

  @pro_20_5_plan = BF.create_rate_plan(pro_plan_id, @create_rate_plan_for_pro_20_5_5)
  @biz_plan = BF.create_rate_plan(biz_plan_id, @create_rate_plan_for_biz_monthly)
end

但它似乎不起作用。 create_products方法中的变量不可用于其他类方法。

我实际上并不是很清楚如何原则上使用方法/类和变量。

1 个答案:

答案 0 :(得分:0)

类方法中的实例变量(使用单个commit1 HEAD commit2 commit3 commit4 ... )或类的实例eval存储在类中。

@