Instructions
Create a Greetable module with:
- A
greetmethod that returns "Hello, I'm #{name}!" - Assumes the class has a
namemethod
Then create a Person class that:
- Includes the Greetable module
- Has a name attribute set in initialize
Hints:
- Define module with: module Greetable
- Use include Greetable in the class
- The module can call methods from the class
Your Code
module Greetable
def greet
"Hello, I'm #{name}!"
end
end
class Person
include Greetable
attr_reader :name
def initialize(name)
@name = name
end
end
RSpec.describe Person do
let(:person) { Person.new("Alice") }
it "has a name" do
expect(person.name).to eq("Alice")
end
it "can greet" do
expect(person.greet).to eq("Hello, I'm Alice!")
end
it "includes Greetable" do
expect(Person.included_modules).to include(Greetable)
end
end
Results
Click "Run Tests" to see results
1 / 2
Next: Comparable Module