0
0
Rubyprogramming~10 mins

Pure functions concept 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 define a pure function that returns the square of a number.

Ruby
def square(num)
  return 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 '+' instead of '*' will add the number to itself, not square it.
Using '/' or '-' will not produce the square.
2fill in blank
medium

Complete the code to define a pure function that returns the length of a string.

Ruby
def length_of_string(str)
  return str.[1]
end
Drag options to blanks, or click blank then click option'
Alength
Bsize
Ccount
Dchars
Attempts:
3 left
💡 Hint
Common Mistakes
Using chars returns an array of characters, not the length.
Using count requires an argument and is not the best choice here.
3fill in blank
hard

Fix the error in the pure function that returns the sum of two numbers.

Ruby
def add_numbers(a, b)
  return 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 '-' subtracts instead of adds.
Using '*' or '/' changes the operation entirely.
4fill in blank
hard

Complete the code to create a pure function that returns a hash with word lengths for words longer than 3 characters.

Ruby
def word_lengths(words)
  lengths = {word=> word.length for word in words if word.length [1] 3}
  return lengths
end
Drag options to blanks, or click blank then click option'
A=>
B<=
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '==' in the condition will select wrong words.
Using ':' instead of '=>' in the hash mapping is incorrect syntax.
5fill in blank
hard

Fill all three blanks to create a pure function that returns a hash of uppercase words and their lengths for words longer than 4 characters.

Ruby
def uppercase_word_lengths(words)
  result = { [1] => [2] for word in words if word.length [3] 4 }
  return result
end
Drag options to blanks, or click blank then click option'
Aword.upcase
Bword.length
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of word.upcase will not uppercase the words.
Using '<' or '<=' instead of '>' will select wrong words.