Array#every
class Array def every(&block) return self unless block_given? last = size.nonzero? ? size - 1 : 0 arity = block.arity each_with_index { |e, i| yield(*[e, i == 0, i == last, i].slice(0, arity)) } end end %w(foo bar).every do |item, is_first, is_last, index| # foo => foo, true, false, 0 # bar => bar, false, true, 1 end # or maybe you don't need 'em all %w(foo bar).every { |item, first| ... }
About this entry
You’re currently reading “Array#every,” an entry on geeksomnia
- Published:
- 09.08.07 / 5pm
- Category:
- Code
No comments
Jump to comment form | comments rss [?] | trackback uri [?]