如何在Ruby中构建范围?
发布时间:2020-12-16 23:20:35 所属栏目:百科 来源:网络整理
导读:是否有可能在ruby中使用解构来提取结束并从范围开始? module PriceHelper def price_range_human( range ) "$%s to $%s" % [range.begin,range.end].map(:number_to_currency) endend 我知道我可以使用数组强制作为一个非常糟糕的黑客: first,*center,last
是否有可能在ruby中使用解构来提取结束并从范围开始?
module PriceHelper def price_range_human( range ) "$%s to $%s" % [range.begin,range.end].map(:number_to_currency) end end 我知道我可以使用数组强制作为一个非常糟糕的黑客: first,*center,last = *rng "$%s to $%s" % [first,last].map(:number_to_currency) 但是有没有一种语法方式来获得开始和结束而不实际手动创建一个数组? min,max = (1..10) 本来很棒的. 解决方法
不,在Cary Swoveland,周刊世界新闻或其他小报证明不正确之前,我会继续相信没有任何证据表明答案是“不”;但它很容易制作.
module RangeWithBounds refine Range do def bounds [self.begin,self.end] end end end module Test using RangeWithBounds r = (1..10) b,e = *r.bounds puts "#{b}..#{e}" end 然后,我首先要写“#{r.begin.number_to_currency} ..#{r.end.number_to_currency}”. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |