0
0
Rubyprogramming~10 mins

Implicit return (last expression) 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 return the sum of two numbers without using the return keyword.

Ruby
def add(a, b)
  a [1] b
end
Drag options to blanks, or click blank then click option'
A+
B-
C/
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Trying to use the return keyword explicitly.
2fill in blank
medium

Complete the code to implicitly return the length of the string.

Ruby
def length_of_string(str)
  str.[1]
end
Drag options to blanks, or click blank then click option'
Asize
Bcount
Cchars
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using chars which returns an array, not a number.
Using count without arguments returns 0.
3fill in blank
hard

Fix the error in the method to implicitly return the square of the number.

Ruby
def square(num)
  num [1] 2
end
Drag options to blanks, or click blank then click option'
A**
B*
C^
Dpow
Attempts:
3 left
💡 Hint
Common Mistakes
Using * which multiplies but does not square.
Using ^ which is a bitwise XOR, not exponentiation.
4fill in blank
hard

Fill both blanks to create a method that returns the last character of a string implicitly.

Ruby
def last_char(str)
  str[1][2]
end
Drag options to blanks, or click blank then click option'
A[0]
B.last
C[-1]
D.first
Attempts:
3 left
💡 Hint
Common Mistakes
Using [0] or .first which return the first character.
Mixing index and method incorrectly.
5fill in blank
hard

Fill all three blanks to create a method that returns a hash with keys as uppercase words and values as their lengths, implicitly.

Ruby
def word_lengths(words)
  words.each_with_object([1]) do |word, result|
    result[[2]] = word.[3]
  end
end
Drag options to blanks, or click blank then click option'
A{}
Bword.upcase
Clength
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of an empty hash {}.
Using word instead of word.upcase as keys.
Using size or other methods instead of length.