0
0
Rubyprogramming~10 mins

Why methods always return a value 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 return the sum of two numbers.

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.
2fill in blank
medium

Complete the method to return the last word of a sentence.

Ruby
def last_word(sentence)
  words = sentence.split(' ')
  words.[1]
end
Drag options to blanks, or click blank then click option'
Apop
Bfirst
Cshift
Dlast
Attempts:
3 left
💡 Hint
Common Mistakes
Using first or shift which return the first element.
3fill in blank
hard

Fix the error in the method so it returns the square of a number.

Ruby
def square(num)
  num [1] num
end
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
4fill in blank
hard

Fill the blank to create a method that returns true if a number is even.

Ruby
def even?(num)
  num.[1]
end
Drag options to blanks, or click blank then click option'
Aeven?
B0
C1
Dodd?
Attempts:
3 left
💡 Hint
Common Mistakes
Using odd? instead of even?.
5fill in blank
hard

Fill all three blanks to create a method that returns a hash of word lengths for words longer than 3 letters.

Ruby
def word_lengths(words)
  lengths = {}
  words.each do |word|
    if word.length [3] 3
      lengths[[1]] = [2]
    end
  end
  lengths
end
Drag options to blanks, or click blank then click option'
Aword
Bword.length
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than for filtering.