Complete the code to assign an integer value to the variable.
number = [1]The variable number should be assigned an integer value. 42 is an integer, while 3.14 is a float, "100" is a string, and true is a boolean.
Complete the code to assign a float value to the variable.
pi = [1]The variable pi should be assigned a float value. 3.14 is a float, while 22 is an integer, "3.14" is a string, and true is a boolean.
Fix the error in the code to correctly convert the string to an integer.
number = "123" converted = [1]
to_f which converts to float, not integer.to_s which keeps it as string.To convert a string containing digits to an integer in Ruby, use to_i. to_f converts to float, to_s converts to string, and to_sym converts to symbol.
Fill both blanks to create a hash with word lengths only for words longer than 4 characters.
words = ["apple", "cat", "banana", "dog"] lengths = {word => word.[1] for word in words if word.[2] > 4}
length? which is not a valid method.count which counts characters but is less common here.In Ruby, word.length or word.size returns the number of characters in the string. Here, word.length is used to get the length for the hash value, and word.size is used to check if the word is longer than 4 characters.
Fill all three blanks to create a hash with uppercase words as keys and their float lengths as values for words longer than 3 characters.
words = ["pear", "fig", "grape", "kiwi"] result = { [1] => [2].length.[3] for [2] in words if [2].length > 3 }
length without converting to float.The hash keys are uppercase words using word.upcase. The values are the length converted to float using word.length.to_f. The variable word is used to iterate over the list.