Complete the code to return the sum of two numbers.
def add(a, b) a [1] b end
The + operator adds two numbers, so the method returns their sum.
Complete the method to return the last word of a sentence.
def last_word(sentence) words = sentence.split(' ') words.[1] end
first or shift which return the first element.The last method returns the last element of an array, which is the last word.
Fix the error in the method so it returns the square of a number.
def square(num) num [1] num end
The * operator multiplies the number by itself, returning the square.
Fill the blank to create a method that returns true if a number is even.
def even?(num) num.[1] end
odd? instead of even?.Integers in Ruby have an even? method that returns true if the number is even and false otherwise.
Fill all three blanks to create a method that returns a hash of word lengths for words longer than 3 letters.
def word_lengths(words) lengths = {} words.each do |word| if word.length [3] 3 lengths[[1]] = [2] end end lengths end
This creates a hash where keys are words and values are their lengths, but only for words longer than 3 letters.