Вопрос по ruby – Ruby - каждое начальное смещение

19

Как установить начальное смещение дляeach цикл в рубине? Я хочу, чтобы цикл начался сa[3] вместоa[0], Как мне установить это?

a = [ab, cd, ef, gh, hi, jk]

a.each do |i|
#some stuff
end

Ваш Ответ

5   ответов
1

Benchmark

require 'benchmark/ips'

a = Array.new(1000) { |i| i } 
drop_point = 500

Benchmark.ips do |x|
  x.report('drop(x)') do
    a.drop(drop_point).each { |el| el + 1 }
  end

  x.report('[x...-1]') do 
    a[drop_point..-1].each { |el| el + 1 }
  end

  x.report('for in') do
    n = drop_point
    size = a.size
    for i in n...size
      a[i] + 1
    end
  end

  x.report('while') do
    size = a.size
    i = drop_point
    while i < size
      a[i] + 1
      i += 1
    end
  end

  x.compare!
end
Results
Warming up --------------------------------------
             drop(x)     4.142k i/100ms
            [x...-1]     4.125k i/100ms
              for in     3.239k i/100ms
               while     5.391k i/100ms
Calculating -------------------------------------
             drop(x)     42.485k (± 2.3%) i/s -    215.384k in   5.072524s
            [x...-1]     42.533k (± 2.3%), i/s -    214.500k in   5.045832s
              for in     32.855k (± 2.5%) i/s -    165.189k in   5.030958s
               while     53.489k (± 4.5%) i/s -    269.550k in   5.049793s

Comparison:
               while:    53489.3 i/s
            [x...-1]:    42532.8 i/s - 1.26x  slower
             drop(x):    42484.7 i/s - 1.26x  slower
              for in:    32855.4 i/s - 1.63x  slower
Conclusions

whileblock[x...-1]drop(x)

3

n = 3
for i in n...a.size
  puts a[i]
end
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededdo/endError: User Rate Limit Exceeded
21

each

a[3..-1].each do |i|
  #some stuff
end
Error: User Rate Limit Exceeded
41

Array#drop:

a.drop(3).each do |i|
  # do something with item i
end

Enumerableeach

a = [0,1,2,3,4,5,6,7]
a.drop(3).select(&:even?).map { |x| x * 2 }
# => [8, 12]

a = ["1", "2", "3", "4", "5"]
puts a.drop(3).join("\n")

4
5

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededmap, filter, take, drop, zipError: User Rate Limit ExceededEnumerableError: User Rate Limit Exceeded
4

a[3..-1].each do |i|
  #your logic
end

Похожие вопросы