我有一个类,其实例变量由哈希组成。
需要'hashie'
class CardBrandFees < Hashie::Trash
property :assessments, from: :assessmentss
attr_reader :discover, :visa, :mastercard, :mc_high_ticket, :assessments, :APF
def initialize(ticket)
@ticket = @ticket
@discover = {:assessments => 0.00105, :APF => 0.0195}
@visa = {:assessments => 0.00110, :APF => 0.0195}
@mastercard = {:assessments => 0.00110, :APF => 0.0195}
@mc_high_ticket = {:assessments => 0.00010}
end
end
我想制作一个卡片品牌对象,并能够通过点符号访问这些哈希值,所以:
cardbrand = CardBrandFees.new(ticket)
cardbrand.discover.assessments => 0.00105
我很难完成这个,并且不确定它甚至是设置这个简单课程的最有效方法,我已经尝试了哈希,但是不能让它工作,有没有人有这种情况的简单解决方案?
答案 0 :(得分:1)
require 'ostruct'
class CardBrandFees
attr_reader :discover, :visa, :mastercard, :mc_high_ticket, :assessments, :APF
def initialize(ticket)
@ticket = ticket
@discover = OpenStruct.new(:assessments => 0.00105, :APF => 0.0195)
@visa = OpenStruct.new(:assessments => 0.00110, :APF => 0.0195)
@mastercard = OpenStruct.new(:assessments => 0.00110, :APF => 0.0195)
@mc_high_ticket = OpenStruct.new(:assessments => 0.00010)
end
end
p CardBrandFees.new("ticket").discover.assessments # => 0.00105
p CardBrandFees.new("ticket").mastercard.APF # => 0.0195