0
0
Rubyprogramming~5 mins

Include for instance methods in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the include keyword do in Ruby?
It adds the methods from a module as instance methods to a class, so objects of that class can use those methods.
Click to reveal answer
beginner
How do instance methods from a module become available in a Ruby class?
By using include ModuleName inside the class, the module's instance methods become instance methods of the class.
Click to reveal answer
beginner
Given this module:<br>
module Greet
  def hello
    "Hi!"
  end
end
<br>How do you make hello available to instances of class Person?
Inside the Person class, write include Greet. Then any Person object can call hello.
Click to reveal answer
intermediate
What is the difference between include and extend in Ruby?
<code>include</code> adds module methods as instance methods. <code>extend</code> adds them as class methods.
Click to reveal answer
beginner
Can a Ruby class include multiple modules for instance methods?
Yes, a class can include many modules. All their instance methods become available to the class's objects.
Click to reveal answer
What happens when you use include with a module in a Ruby class?
AThe module is copied as a subclass.
BThe module's methods become class methods of the class.
CNothing happens automatically.
DThe module's methods become instance methods of the class.
Which keyword adds module methods as class methods instead of instance methods?
Aextend
Binclude
Crequire
Dimport
If a module has a method greet, how do you call it on an object of a class that included the module?
Aobject.greet
BClassName.greet
CModuleName.greet
DYou cannot call it
Can a Ruby class include more than one module to get instance methods?
ANo, only one module can be included.
BYes, multiple modules can be included.
COnly if modules are nested.
DOnly if modules have different names.
What is the effect of including a module inside a Ruby class?
AIt creates a new subclass.
BIt copies the module's code into the class.
CIt adds the module's instance methods to the class.
DIt deletes existing methods in the class.
Explain how the include keyword works to add instance methods from a module to a Ruby class.
Think about how a class gets new methods from a module.
You got /4 concepts.
    Describe the difference between include and extend when using modules in Ruby.
    One affects objects, the other affects the class itself.
    You got /3 concepts.