工作中碰到这样的问题,需要处理乘法法则排列组合后的每一种组合。具体说就是:用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.
过几天贴出我的解法。
分享到:
- 2008-03-27 15:09
- 浏览 1360
- 评论(9)
- 论坛回复 / 浏览 (9 / 3646)
- 查看更多
相关推荐
C位出道蓝攻略:新时代内容营销指南(完整版)
采用Python的web开发框架(Django)和Mysql数据库,数据库的设计很不规范,由于时间方面的问题(基于才“出道”时的原有博客数据库结构),图片上传采用Python的Image类库,分页采用的是Paginator,其他的我就不一一列举...
C位出道蓝攻略:新时代内容营销指南(完整版).pdf
C位出道:新时代内容营销指南-蓝色光标-201811.pdf
贝泰妮(A20333)新股研究报告:功能护肤标杆如何C位出道?_
20210301-中泰证券-贝泰妮(A20333)新股研究报告:功能护肤标杆如何C位出道?_.pdf
半路出道的投行家半路出道的投行家半路出道的投行家半路出道的投行家半路出道的投行家半路出道的投行家
医疗器械行业深度研究:个性化出道,国产品牌的奇袭-20200912-天风证券-103页.pdf
《失忆偶像出道中》步临风第一年攻略.docx
贴膜机器人“出道”.pdf
凡士林&杨洋出道11周年粉丝见面会方案.pdf
數據庫導出道入命令匯總﹐ORACLE函数大全 等各種資料匯總
oCP“C”位出道攻略.pdf
顶流男团「CCTVBoys」,请立即出道!.pdf
国外创业公司刚出道时 是否遭遇迅速抄袭?.docx
《失忆偶像出道中》冷月心第一年攻略.docx
宏观行业研究方法The Accidental Investment Banker 半路出道的投行家.pdf
此篇日志为出马、出道有难人能减轻些痛苦.docx