我也来出道rubyquiz:each_component

工作中碰到这样的问题,需要处理乘法法则排列组合后的每一种组合。具体说就是:用Hash表示一套组合,通过each_component把其中的每一种可能的组合抽出来,作为一个新的Hash,在block中使用。

改编成一个rubyquiz。要求:扩展Hash类,写一个each_component方法,让下面的代码可以运行出期望的结果来。

class Sneaker
def initialize(attributes)
@brand, @size, @store = attributes[:brand], attributes[:size], attributes[:store]
end

def to_s
"#{@brand} sneakers of #{@size}in from #{@store} store."
end
end

sneaker_combination = {
:brand => [:adidas, :nike],
:size => [38, 40, 42],
:store => :shanghai
}

sneaker_combination.each_component do |sneaker|
# sneaker_sample.should.be({:brand => :nike, :size => 38, :store => :shanghai})
puts Sneaker.new(sneaker)
end
# should print:
# adidas sneakers of 38in from shanghai store.
# adidas sneakers of 40in from shanghai store.
# adidas sneakers of 42in from shanghai store.
# nike sneakers of 38in from shanghai store.
# nike sneakers of 40in from shanghai store.
# nike sneakers of 42in from shanghai store.


过几天贴出我的解法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值