0
0
Rubyprogramming~10 mins

Explicit return statement 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 explicitly return the sum of two numbers.

Ruby
def add(a, b)
  [1] a + b
end
Drag options to blanks, or click blank then click option'
Aputs
Bprint
Creturn
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Using print or puts instead of return, which only outputs but does not return a value.
2fill in blank
medium

Complete the code to explicitly return the string 'Hello' from the method.

Ruby
def greet
  [1] "Hello"
end
Drag options to blanks, or click blank then click option'
Aprint
Breturn
Cputs
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Using puts or print which only display output but do not return a value.
3fill in blank
hard

Fix the error in the method to explicitly return the product of two numbers.

Ruby
def multiply(x, y)
  product = x * y
  [1] product
end
Drag options to blanks, or click blank then click option'
Aputs
Byield
Cprint
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using puts or print which only output the value but do not return it.
4fill in blank
hard

Fill both blanks to explicitly return the length of the string if it is greater than 5.

Ruby
def check_length(str)
  if str.length > 5
    [1] str.length
  else
    [2] 0
  end
end
Drag options to blanks, or click blank then click option'
Areturn
Bputs
Cprint
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Using puts or print which only display output but do not return a value.
5fill in blank
hard

Fill all three blanks to explicitly return a hash with keys as uppercase letters and values as their lengths for words longer than 3 letters.

Ruby
def word_lengths(words)
  result = {}
  words.each do |word|
    if word.length > 3
      result[[1]] = [2]
      [3] result
    end
  end
  result
end
Drag options to blanks, or click blank then click option'
Aword.upcase
Bword.length
Creturn
Dword.downcase
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.downcase instead of word.upcase for the key.
Not using return to explicitly return the hash inside the loop.