I have a basic ruby loop
for video in site.posts
video.some_parameter
endfor
I want to run this loop 2 or 3 times.
Is this possible?
Loops are a fundamental concept in any programming language. They allow us to execute a specific action continuously as long as a specified condition is true. Ruby also offers the concept of loops that can perform similar actions.
The Ruby Enumerable#each method is the most simplistic and popular way to iterate individual items in an array. It accepts two arguments: the first being an enumerable list, and the second being a block. It takes each element in the provided list and executes the block, taking the current item as a parameter.
3.times do
# do work here
end
check http://www.tutorialspoint.com/ruby/ruby_loops.htm
If you need an index:
5.times do |i|
print i, " "
end
Returns:
0 1 2 3 4
Reference: https://apidock.com/ruby/Integer/times
It's bad style to use for.
3.times do
site.posts.each do |video|
video.some_parameter
end
end
or if video.some_parameter is one line,
3.times do
site.posts.each { |video| video.some_parameter }
end
see: https://github.com/bbatsov/ruby-style-guide#source-code-layout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With