0
0
Rubyprogramming~10 mins

Include for instance methods 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 for instance methods.

Ruby
module Greetings
  def hello
    "Hello!"
  end
end

class Person
  [1] Greetings
end

p = Person.new
puts p.hello
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' which adds methods as class methods.
Using 'require' or 'import' which are for loading files or libraries.
2fill in blank
medium

Complete the code to call the instance method from the included module.

Ruby
module Farewell
  def goodbye
    "Goodbye!"
  end
end

class Friend
  include Farewell
end

f = Friend.new
puts f.[1]
Drag options to blanks, or click blank then click option'
Afarewell
Bgoodbye
Chello
Dbye
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method not defined in the module.
Using a method name that does not exist on the instance.
3fill in blank
hard

Fix the error by completing the code to include the module correctly.

Ruby
module Helper
  def assist
    "Assisting"
  end
end

class Worker
  [1] Helper
end

w = Worker.new
puts w.assist
Drag options to blanks, or click blank then click option'
Aextend
Bimport
Cinclude
Dprepend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' which adds methods as class methods.
Using 'import' which is not a Ruby keyword.
4fill in blank
hard

Fill both blanks to define a module and include it for instance methods.

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

class User
  [2] GreetModule
end

u = User.new
puts u.greet
Drag options to blanks, or click blank then click option'
AGreetModule
Bextend
Cinclude
DGreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' instead of 'include' to add instance methods.
Using a module name that does not match the definition.
5fill in blank
hard

Fill all three blanks to create a module, include it, and call its instance method.

Ruby
module [1]
  def welcome
    "Welcome!"
  end
end

class Member
  [2] WelcomeModule
end

m = Member.new
puts m.[3]
Drag options to blanks, or click blank then click option'
AWelcomeModule
Binclude
Cwelcome
Dextend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extend' instead of 'include' to add instance methods.
Calling a method name that does not exist in the module.
Mismatching module name in definition and inclusion.