Bird
Raised Fist0
Rubyprogramming~10 mins

Pure functions concept in Ruby - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following best describes a pure function in Ruby?
easy
A. A function that reads user input and prints output directly.
B. A function that always returns the same output for the same input and has no side effects.
C. A function that modifies global variables to keep track of state.
D. A function that changes the value of its arguments.

Solution

  1. Step 1: Understand pure function definition

    A pure function always returns the same result when given the same input and does not affect anything outside itself.
  2. Step 2: Compare options to definition

    A function that always returns the same output for the same input and has no side effects. matches this definition exactly. Options A, B, and D involve side effects or changing external state.
  3. Final Answer:

    A function that always returns the same output for the same input and has no side effects. -> Option B
  4. Quick Check:

    Pure function = consistent output + no side effects [OK]
Hint: Pure functions never change outside data and always return same output [OK]
Common Mistakes:
  • Thinking functions that modify variables are pure
  • Confusing input/output consistency with side effects
  • Assuming printing output is allowed in pure functions
2. Which Ruby function below is a pure function?
easy
A. def add(a, b); a + b; end
B. def add(a, b); puts a + b; end
C. def add(a, b); @sum = a + b; end
D. def add(a, b); print a + b; end

Solution

  1. Step 1: Check each function for side effects and output consistency

    def add(a, b); a + b; end returns sum without changing anything outside or printing. def add(a, b); puts a + b; end prints (side effect). def add(a, b); @sum = a + b; end changes instance variable (side effect). def add(a, b); print a + b; end prints (side effect).
  2. Step 2: Identify pure function

    Only def add(a, b); a + b; end meets pure function rules: same output for same input, no side effects.
  3. Final Answer:

    def add(a, b); a + b; end -> Option A
  4. Quick Check:

    Pure function = no side effects + returns value [OK]
Hint: Pure functions return values without printing or changing variables [OK]
Common Mistakes:
  • Confusing printing output with returning value
  • Assuming modifying instance variables is allowed
  • Assuming print doesn't cause side effects
3. What is the output of this Ruby code?
def multiply(x, y)
  x * y
end

result = multiply(3, 4)
puts result
medium
A. Error
B. nil
C. 12
D. 7

Solution

  1. Step 1: Understand the multiply function

    The function returns the product of x and y, so multiply(3, 4) returns 12.
  2. Step 2: Check the output statement

    puts prints the returned value, so it prints 12.
  3. Final Answer:

    12 -> Option C
  4. Quick Check:

    multiply(3,4) = 12 [OK]
Hint: Pure functions return predictable output; multiply(3,4) = 12 [OK]
Common Mistakes:
  • Thinking function returns nil if no explicit return
  • Confusing addition with multiplication
  • Expecting error due to missing return keyword
4. Identify the error in this Ruby function that is supposed to be pure:
def increment(n)
  n += 1
  puts n
end
medium
A. It modifies the input variable n.
B. It uses invalid syntax for increment.
C. It does not return any value explicitly.
D. It prints output, causing a side effect.

Solution

  1. Step 1: Check for side effects

    The function prints the value of n, which is a side effect because it affects outside world.
  2. Step 2: Verify other aspects

    Modifying n locally is allowed; syntax is correct; implicit return is allowed but printing breaks purity.
  3. Final Answer:

    It prints output, causing a side effect. -> Option D
  4. Quick Check:

    Pure functions must not print or cause side effects [OK]
Hint: Printing inside function breaks purity due to side effects [OK]
Common Mistakes:
  • Thinking local variable change breaks purity
  • Assuming implicit return is an error
  • Ignoring side effects caused by printing
5. You want to write a pure function in Ruby that takes a list of numbers and returns a new list with each number doubled. Which implementation is pure?
hard
A. def double_list(nums) nums.map { |n| n * 2 } end
B. def double_list(nums) nums.collect! { |n| n *= 2 } nums end
C. def double_list(nums) nums.map! { |n| n * 2 } end
D. def double_list(nums) nums.clear nums.push(2, 4, 6) end

Solution

  1. Step 1: Understand pure function requirements

    A pure function returns new data without changing input or external state.
  2. Step 2: Analyze each option

    def double_list(nums) nums.collect! { |n| n *= 2 } nums end uses collect! which modifies original array (side effect). def double_list(nums) nums.map { |n| n * 2 } end returns a new array with doubled values without changing original. def double_list(nums) nums.map! { |n| n * 2 } end uses map! which modifies original array (side effect). def double_list(nums) nums.clear nums.push(2, 4, 6) end clears and modifies original array (side effect).
  3. Final Answer:

    def double_list(nums) nums.map { |n| n * 2 } end -> Option A
  4. Quick Check:

    Pure function = no input change + returns new data [OK]
Hint: Use non-destructive methods like map, avoid modifying input [OK]
Common Mistakes:
  • Using destructive methods like map! or clear
  • Modifying input array instead of returning new one
  • Confusing each with map for transformation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard