Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach (array/hash as $key => $value)
- array.length.times do |i|
- end
- array.each
- array.each_index
- for i in array
- hash.each do |key, value|
- array = [1, 2, 3, 4, 5, 6]
- array.each { |x| puts x }
- 1
- 2
- 3
- 4
- 5
- 6
- array = ["A", "B", "C"]
- array.each_with_index {|x, y| puts "#{x} => #{y}" }
- A => 0
- B => 1
- C => 2
- for index in 0 ... array.size
- puts "array[#{index}] = #{array[index].inspect}"
- end
- for key in hash.keys.sort
- puts "hash[#{key.inspect}] = #{hash[key].inspect}"
- end
- (1..10).each{|i| puts "i=#{i}" }
- (1..10).to_a.reverse.each{|i| puts "i=#{i}" }
- (a=*1..10).each{|i| puts "i=#{i}" }
- (a=*1..10).reverse.each{|i| puts "i=#{i}" }
- (*1..10).each{|i| puts "i=#{i}" }
- (*1..10).reverse.each{|i| puts "i=#{i}" }
- #
- (1..10).step(1){|i| puts "i=#{i}" }
- (1..10).step(-1){|i| puts "i=#{i}" }
- #
- (1..10).each{|i| puts "i=#{i}" }
- (10..1).each{|i| puts "i=#{i}" } # I don't want this though. It's dangerous
- class Range
- def each_reverse(&block)
- self.to_a.reverse.each(&block)
- end
- end
- ary.each_with_index { |val, idx| # ...
- class Hash
- def each_pairwise
- self.each { | x, y |
- yield [x, y]
- }
- end
- end
- class Array
- def each_pairwise
- self.each_with_index { | x, y |
- yield [y, x]
- }
- end
- end
- ["a","b","c"].each_pairwise { |x,y|
- puts "#{x} => #{y}"
- }
- {"a" => "Aardvark","b" => "Bogle","c" => "Catastrophe"}.each_pairwise { |x,y|
- puts "#{x} => #{y}"
- }
- ['Home', '/', 'Page two', 'two', 'Test', 'test'].each_slice(2) do|label,link|
- li {a label, :href => link}
- end
- require 'enumerator'
- ['a',1,'b',2].to_a.flatten.each_slice(2) {|x,y| puts "#{x} => #{y}" }
- # is equivalent to...
- {'a'=>1,'b'=>2}.to_a.flatten.each_slice(2) {|x,y| puts "#{x} => #{y}" }
- # Ruby 1.8
- [1,2,[1,2,3]].flatten
- => [1,2,1,2,3]
- # Ruby 1.9
- [1,2,[1,2,3]].flatten(0)
- => [1,2,[1,2,3]]
Add Comment
Please, Sign In to add comment