0
0
Rubyprogramming~10 mins

Why operators are methods in Ruby - Test Your Understanding

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

Complete the code to add two numbers using the plus operator method.

Ruby
result = 5.[1](3)
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' or '*' instead of '+' will give wrong results.
Forgetting that operators are methods and need parentheses.
2fill in blank
medium

Complete the code to check equality using the '==' operator method.

Ruby
is_equal = 'ruby'.[1]('ruby')
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' reverses the logic.
Using '<' or '>' compares order, not equality.
3fill in blank
hard

Fix the error in the code by replacing the blank with the correct operator method to multiply.

Ruby
product = 4.[1](5)
Drag options to blanks, or click blank then click option'
A*
B-
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' will add or subtract instead of multiply.
Using '/' divides instead of multiplying.
4fill in blank
hard

Fill both blanks to create a hash that maps numbers to their squares using operator methods.

Ruby
squares = {1[1]1, 2[2]4}
Drag options to blanks, or click blank then click option'
A=>
B:
C,
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' instead of '=>' is valid only for symbol keys, not numbers.
Using ',' or '-' between key and value causes syntax errors.
5fill in blank
hard

Fill all three blanks to define a method that adds two numbers using operator methods.

Ruby
def add(a, b)
  a[1]b
end

sum = add([2], [3])
Drag options to blanks, or click blank then click option'
A+
B5
C3
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' changes addition to subtraction.
Passing arguments in wrong order or missing them causes errors.