0
0
Rubyprogramming~10 mins

Custom modules as mixins 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 include the module as a mixin in the class.

Ruby
module Greetings
  def greet
    "Hello!"
  end
end

class Person
  [1] Greetings
end
Drag options to blanks, or click blank then click option'
Aimport
Bextend
Crequire
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' instead of 'include' adds methods as class methods, not instance methods.
Using 'require' or 'import' does not mix in methods.
2fill in blank
medium

Complete the code to call the mixin method from an instance.

Ruby
module Logger
  def log(message)
    "Log: #{message}"
  end
end

class App
  include Logger
end

app = App.new
puts app.[1]("Started")
Drag options to blanks, or click blank then click option'
Aprint
Blog
Cputs
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call 'print' or 'puts' as the method from the module.
Using 'message' which is a parameter, not a method.
3fill in blank
hard

Fix the error by choosing the correct keyword to add module methods as class methods.

Ruby
module Tools
  def info
    "Tool info"
  end

  def details
    "Details"
  end
end

class Machine
  [1] Tools
end

puts Machine.info
Drag options to blanks, or click blank then click option'
Aextend
Binclude
Crequire
Dimport
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'include' adds instance methods, so 'Machine.info' would fail.
Using 'require' or 'import' does not add methods to the class.
4fill in blank
hard

Fill both blanks to create a mixin module and include it in a class.

Ruby
module [1]
  def shout
    "Hey!"
  end
end

class [2]
  include Shoutable
end
Drag options to blanks, or click blank then click option'
AShoutable
BTalker
CSpeaker
DListener
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatch between module name and the name used in include.
Using unrelated class names.
5fill in blank
hard

Fill all three blanks to define a module with an instance method, include it in a class, and call the method.

Ruby
module [1]
  def greet
    "Hi!"
  end
end

class [2]
  [3] Greetings
end

obj = Greeter.new
puts obj.greet
Drag options to blanks, or click blank then click option'
AGreetings
BGreeter
Cinclude
Dextend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' instead of 'include' for instance methods.
Mismatch in module or class names.