You can use the slice
method to select the first two elements of an array. Then, you can use a loop to iterate over them and convert their index numbers into digits. Here's an example:
main.rb376 chars14 lines
Output:
main.rb46 chars3 lines
In this example, the slice
method is used to select the first two elements of the array. Then, the each_with_index
method is used to loop over the selected elements, providing both the element value and its index. The format
method is used to convert the index number into a two-digit string, and then the digit and corresponding element are printed inside the loop.
gistlibby LogSnag