Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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_Oop_Ruby On Rails 4 - Fatal编程技术网

理解ruby中的方法调用

理解ruby中的方法调用,ruby,oop,ruby-on-rails-4,Ruby,Oop,Ruby On Rails 4,我有两节课 class Cart belongs_to :coupon end class Coupon has_many :carts def discount end end 我执行 cart = Cart.last.coupon.discount 如果不将购物车作为参数传递,我如何知道折扣方法中的购物车?您不能从优惠券中选择购物车。我建议您在购物车内给予折扣 class Cart belongs_to :coupon delegate :discount,

我有两节课

class Cart
  belongs_to :coupon
end

class Coupon
  has_many :carts

  def discount
  end
end
我执行

cart = Cart.last.coupon.discount

如果不将购物车作为参数传递,我如何知道折扣方法中的购物车?

您不能从优惠券中选择购物车。我建议您在购物车内给予折扣

class Cart
  belongs_to :coupon
  delegate :discount, to: coupon
end

class Coupon
  has_many :carts

  def discount
  end
end
那你可以

discount = cart.discount
但是,请注意,委托是rails方法。我假设,当您使用属于并标记了RubyonRails的ruby时,您就在rails中。但是,如果没有,这也会起作用:

class Cart
  belongs_to :coupon

  def discount
    coupon.discount if coupon
  end
end

你不能从优惠券中扣除。我建议您在购物车内给予折扣

class Cart
  belongs_to :coupon
  delegate :discount, to: coupon
end

class Coupon
  has_many :carts

  def discount
  end
end
那你可以

discount = cart.discount
但是,请注意,委托是rails方法。我假设,当您使用属于并标记了RubyonRails的ruby时,您就在rails中。但是,如果没有,这也会起作用:

class Cart
  belongs_to :coupon

  def discount
    coupon.discount if coupon
  end
end

这将是我的方法:

class Cart
  belongs_to :coupon

  def discount
    coupon.discount(self) unless coupon.nil?
  end
end

class Coupon
  has_many :carts

  def discount(cart)
    # use the coupon discount logic to apply discount on particular cart
  end
end

这将是我的方法:

class Cart
  belongs_to :coupon

  def discount
    coupon.discount(self) unless coupon.nil?
  end
end

class Coupon
  has_many :carts

  def discount(cart)
    # use the coupon discount logic to apply discount on particular cart
  end
end

这是做不到的,好吧,即使你能做到,那也将是一次可怕而脆弱的黑客攻击。当您使用折扣方式时,您将无法再访问特定购物车,只能访问与优惠券关联的所有购物车。如果那只是一辆车,你就很幸运了

为了减少耦合,更好地遵守德米特定律,我建议在购物车上创建一种折扣方法

def discount 
  coupon.discount(self)
end
另一个选择可能是有另一个活动类,我们称之为CouponActivation,坐在中间,把手推车和优惠券联系起来,计算折扣本身。p> Cart属于CouponActivation,CouponActivation有一个Cart,属于优惠券,优惠券有多个CouponActivation,通过CouponActivation有多个Cart。您将折扣方法置于耦合激活状态,并可以访问所需的信息


仍然建议只传递值。更容易,更清晰,更容易测试

这是做不到的,亲爱的,即使你能做到,那也将是一次可怕而脆弱的黑客攻击。当您使用折扣方式时,您将无法再访问特定购物车,只能访问与优惠券关联的所有购物车。如果那只是一辆车,你就很幸运了

为了减少耦合,更好地遵守德米特定律,我建议在购物车上创建一种折扣方法

def discount 
  coupon.discount(self)
end
另一个选择可能是有另一个活动类,我们称之为CouponActivation,坐在中间,把手推车和优惠券联系起来,计算折扣本身。p> Cart属于CouponActivation,CouponActivation有一个Cart,属于优惠券,优惠券有多个CouponActivation,通过CouponActivation有多个Cart。您将折扣方法置于耦合激活状态,并可以访问所需的信息


仍然建议只传递值。更容易,更清晰,更容易测试

我认为你采取的方法不好。 如果优惠券是对象,它可以给您购物车总价的折扣,那么我将使用以下方法:

class Cart
  has_and_belongs_to_many :coupons

  def calculate_discount
    #this is place where you can get rid of double coupons or coupons that are not allowed together
    for coupon in coupons
      coupon.apply_discount_to(self)
    end
  end
end

class Coupon
  has_and_belongs_to_many :carts
end

我认为你采取的方法不好。 如果优惠券是对象,它可以给您购物车总价的折扣,那么我将使用以下方法:

class Cart
  has_and_belongs_to_many :coupons

  def calculate_discount
    #this is place where you can get rid of double coupons or coupons that are not allowed together
    for coupon in coupons
      coupon.apply_discount_to(self)
    end
  end
end

class Coupon
  has_and_belongs_to_many :carts
end

您所说的“没有将购物车作为参数传递的情况下它是哪个购物车”是什么意思?通过问题描述,无论您试图实现什么,您都很可能接近它wrong@AndreyDeineko我觉得它不应该是Cart.last.优惠券.discount(Cart.last)您所说的“没有将Cart作为参数传递的情况下它是哪个Cart”是什么意思?通过问题描述,无论你试图实现什么,你都很可能接近它wrong@AndreyDeineko我觉得它不应该是Cart.last.优惠券.折扣(Cart.last)