访问其他类的变量 [英] Accessing variables of other classes

查看:25
本文介绍了访问其他类的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在上一篇博文中得到了一些很好的帮助(rspec 测试中的未定义方法) 但我只是想寻求更多帮助.

I received some excellent help on my last post (Undefined Method in rspec testing) but I was just looking for a bit more help.

我有一个 rspec 集成规范,我基本上需要更改代码以获得所需的结果.我无法更改规范,因为它是练习的一部分.

I have an rspec integration spec that I basically need to alter code for to get the desired outcome. I cannot alter the spec as it's part of the exercise.

let(:user) { User.new(voucher) }

context 'no voucher' do
  let(:voucher) { nil }

  it 'should bill default price all the time' do
      user.bill
      expect(user.orders[0].billed_for).to eql 6.95
      ... ...
  end
end

context 'vouchers' do
  describe 'default vouchers' do
    let(:voucher) { Voucher.create(:default, credit: 15) }

    it 'should not bill user if has a remaining credit' do
      user.bill
      expect(user.orders[0].billed_for).to eql 0.0
      ... ...
    end
  end

我放了一些点只是为了去掉不必要的代码.

I placed some dots just to cut out the unnecessary code.

我非常了解这里发生了什么.

I pretty much understand what's happen here.

正在创建一个新的用户类并在 let 中设置为 :user.然后根据上下文初始化并传入凭证.第一次考试没有设置优惠券.一个设置为第二个.

A new user class is being create and set to :user in let. A voucher is then initiliased and passed in depending on the context. no voucher is set for the first test. One is set for the second.

我的问题从这里开始

require 'order'
require 'voucher'

class User
  attr_accessor :voucher, :orders

  def initialize(orders = [], voucher = nil)
    @voucher = voucher
    @orders = [orders]
  end

  def bill
    new_order = Order.new(self)
    @orders << new_order
  end
end

该方法已初始化.它有可选参数.我有点不清楚初始化是如何工作的,因为我无法在任何地方访问这些设置变量.

The method is initliased. It has optional parameters. I'm a little unclear on how the initialisation works though as I'm unable to access these set variables at all anywhere.

我对范围限制有点不确定,因为我希望从当前看起来像这样的订单类中访问一些凭证变量

I'm a little unsure about the scope limitations though as I'm hoping to access some of the vouchers variables from the order class which currently looks like this

class Order
  DEFAULT_PRICE = 6.95

  attr_accessor :user

  def initialize(user)
    @user = user
  end

  def billed_for
    price = DEFAULT_PRICE
    user.orders.each do |order|
        price - order.billed_for
    end
    price
  end
end

访问用户凭证类应该像 user.voucher 一样简单.??

shoudl accessing the users voucher class be as easy as user.voucher. ??

还有一个小问题.我目前正在使用工厂方法,以便凭证类可以自行初始化.

Also a smaller question. I'm currently using a factory method so the voucher class can initialise itself.

 def self.create(type, *attrs)

*attrs 参数本质上是一个数组.我可以遍历这个并通过检查它们的存在将它绑定到一些预期的变量.即,如果数组具有某个键,则将此键的值设置为变量.这是最好的方式还是有另一种流行的方式?

the *attrs parameter is essentially an array. I can loop through this and bind it to some expected variables by checking for their presence. ie if array has certain key set this key's value to a variable. Is this the best way or is there another popular way?

我知道有很多问题要问,但我发现自己真的很困惑,如果有人能帮我解决这些问题,我将不胜感激.谢谢.

I know this is a lot to ask but I'm finding myself really confused and would be grateful if annyone could clear any of this up me. Thanks.

推荐答案

原来我以错误的顺序初始化变量.这意味着我正在寻找错误类别中的项目.

Turns out I was initializing the variables in the wrong order. This meant that I was looking for items in the wrong class.

令人尴尬的菜鸟错误!一切都会好起来的:)

Embarrassing noob mistake! All the better for it :)

这篇关于访问其他类的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆