0
0
Rubyprogramming~10 mins

Type checking with .class and .is_a? 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 check if the variable is exactly an Integer using .class.

Ruby
num = 10
puts num.class == [1]
Drag options to blanks, or click blank then click option'
AFixnum
BInteger
CNumber
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' or 'Fixnum' which are not valid class names in modern Ruby.
2fill in blank
medium

Complete the code to check if the variable is a kind of Numeric using .is_a?.

Ruby
value = 3.14
puts value.[1](Numeric)
Drag options to blanks, or click blank then click option'
Akind_of?
Binstance_of?
Cis_a?
Dclass_of?
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance_of? which checks only exact class, not subclasses.
3fill in blank
hard

Fix the error in the code to correctly check if 'text' is a String using .class.

Ruby
text = "hello"
if text.class [1] String
  puts "It's a string!"
end
Drag options to blanks, or click blank then click option'
AString
Bequals
Cis_a?
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using single equals = instead of double equals == for comparison.
4fill in blank
hard

Fill both blanks to create a hash of words and their lengths, including only words longer than 3 characters.

Ruby
words = ["cat", "house", "dog", "elephant"]
lengths = {word: word.[1] for word in words if word.[1] [2] 3}
Drag options to blanks, or click blank then click option'
Alength
Bsize
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.length is valid but not in options; using < instead of >.
5fill in blank
hard

Fill all three blanks to create a hash with uppercase words as keys and their lengths as values, including only words with length greater than 4.

Ruby
words = ["apple", "bat", "carrot", "dog"]
result = { [1]: [2] for w in words if [2] [3] 4 }
Drag options to blanks, or click blank then click option'
Aw.upcase
Bw.length
C>
Dw.downcase
Attempts:
3 left
💡 Hint
Common Mistakes
Using w.downcase instead of w.upcase for keys; using < instead of >.