0
0
Rubyprogramming~10 mins

Integer and Float number types in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign an integer value to the variable.

Ruby
number = [1]
Drag options to blanks, or click blank then click option'
A3.14
B"100"
C42
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a float value instead of an integer.
Putting the number inside quotes, making it a string.
2fill in blank
medium

Complete the code to assign a float value to the variable.

Ruby
pi = [1]
Drag options to blanks, or click blank then click option'
A3.14
B"3.14"
C22
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer instead of a float.
Using quotes around the number, making it a string.
3fill in blank
hard

Fix the error in the code to correctly convert the string to an integer.

Ruby
number = "123"
converted = [1]
Drag options to blanks, or click blank then click option'
Anumber.to_sym
Bnumber.to_f
Cnumber.to_s
Dnumber.to_i
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_f which converts to float, not integer.
Using to_s which keeps it as string.
4fill in blank
hard

Fill both blanks to create a hash with word lengths only for words longer than 4 characters.

Ruby
words = ["apple", "cat", "banana", "dog"]
lengths = {word => word.[1] for word in words if word.[2] > 4}
Drag options to blanks, or click blank then click option'
Alength
Bsize
Clength?
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using length? which is not a valid method.
Using count which counts characters but is less common here.
5fill in blank
hard

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.

Ruby
words = ["pear", "fig", "grape", "kiwi"]
result = { [1] => [2].length.[3] for [2] in words if [2].length > 3 }
Drag options to blanks, or click blank then click option'
Aword.upcase
Bword
Cto_f
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using length without converting to float.
Using wrong variable names or missing the iteration variable.