In Ruby, everything is an object, including numbers. The integers are objects, meaning, they are defined from an Integer class that contains methods. Those methods can be called on an instance of the object. Thus, the number 10
is an instance object of the Integer class and you can call methods on it.
One of those methods is times. When you do:
10.times ...
You are calling the times
method on the object 10
. This method accepts a block parameter {...}
:
10.times { |i| puts i }
Here, the i
is the parameter for the block and the times
method internally has a loop that runs that parameter from 0
to the object integer you called the times method on.
As @MarekLipka said in a comment, see the documentation for the Ruby Integer class.