0
0
Rubyprogramming~15 mins

Is_a? and kind_of? for type checking in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Is_a? and kind_of? for type checking
📖 Scenario: Imagine you are building a simple program that needs to check what kind of objects it is working with. This is like sorting your toys by type: cars, dolls, or blocks. In Ruby, you can use is_a? and kind_of? methods to check an object's type.
🎯 Goal: You will create a few objects of different types and then use is_a? and kind_of? to check their types and print the results.
📋 What You'll Learn
Create variables with different types of objects
Use is_a? or kind_of? to check object types
Print the results clearly
💡 Why This Matters
🌍 Real World
Type checking helps programs behave differently depending on the kind of data they get, like checking if a user input is a number or text.
💼 Career
Understanding type checking is important for debugging and writing safe code in Ruby, which is used in web development and scripting.
Progress0 / 4 steps
1
Create variables with different types
Create three variables: number with the value 10, text with the value "hello", and list with the value [1, 2, 3].
Ruby
Need a hint?

Use simple assignment like variable = value.

2
Add type checks using is_a?
Use is_a? to check if number is an Integer, text is a String, and list is an Array. Store the results in variables check_number, check_text, and check_list respectively.
Ruby
Need a hint?

Use variable.is_a?(ClassName) to check the type.

3
Add type checks using kind_of?
Use kind_of? to check if number is a Numeric, text is a Object, and list is a Enumerable. Store the results in variables kind_number, kind_text, and kind_list respectively.
Ruby
Need a hint?

Use variable.kind_of?(ClassName) similar to is_a?.

4
Print the results
Print the values of check_number, check_text, check_list, kind_number, kind_text, and kind_list each on its own line.
Ruby
Need a hint?

Use puts variable to print each result on its own line.