14. Extend your knowledge


#1

This exercise seeks to explain the difference between using include and extend in terms of application of modules to classes. It states that include allows you to apply a module's methods at the instance level, and that extends allows it at the class level.

Can someone explain more? I the exercise, i substituted 'extend' for 'include' in the code and you get exactly the same result:

module ThePresent
  def now
    puts "It's #{Time.new.hour > 12 ? Time.new.hour - 12 : Time.new.hour}:#{Time.new.min} #{Time.new.hour > 12 ? 'PM' : 'AM'} (GMT)."
  end
end

class TheHereAnd
  include ThePresent  <<---- changed to include from extend
end

TheHereAnd.now

#2

Hi @spacecamel100 ,

During an editing session for a given exercise, Codecademy's system remembers names of variables and functions, even after the user has made and submitted changes. This can cause things to happen that would not ordinarily occur if the user were working with a copy of a Ruby or Python interpreter on a local computer.

That is the case here. If you submit this ...

class TheHereAnd
  extend ThePresent
end

TheHereAnd.now

... then edit it, and submit this ...

class TheHereAnd
  include ThePresent
end

TheHereAnd.now

... Codecademy's system will still remember what was previously contributed to the TheHereAnd class by the the extend, and you will not see an error message.

However, if you refresh the page, you'll clean the slate, and then after you submit the code again, you'll see this in the console ...

undefined method `now' for Context::TheHereAnd:Class