Complete the code to print "Hello" 3 times using the times method.
3.[1] do puts "Hello" end
each which is for collections, not numbers.loop which creates an infinite loop.repeat which is not a Ruby method.The times method runs the block the specified number of times. Here, 3.times runs the block 3 times.
Complete the code to print numbers from 0 to 4 using the times method.
5.[1] do |i| puts i end
each which is for collections, not integers.loop which does not yield iteration count.repeat which is not a Ruby method.The times method yields numbers from 0 up to one less than the number. Here, it yields 0 to 4.
Fix the error in the code to print "Hi" 4 times using the times method.
4.times [1] puts "Hi" end
while or for which are different loop constructs.The times method requires a do or curly braces {} to start the block. Here, do is correct.
Fill both blanks to create a hash with numbers as keys and their squares as values using times.
squares = {}
5.[1] do |i|
squares[i] = i[2]2
end+ instead of exponentiation.each instead of times.times repeats the block 5 times with i from 0 to 4. The square is calculated with i**2.
Fill all three blanks to create a hash with uppercase letters as keys and their index as values using times.
letters = {}
26.[1] do |i|
letters[('A'.ord[2] i).[3]] = i
endord instead of chr to convert back to letter.each instead of times.ord and chr.times repeats 26 times. 'A'.ord gives ASCII code of 'A'. Adding i moves through letters. .chr converts code back to letter.