Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:50:39+00:00 2026-06-12T23:50:39+00:00

In the the following tea timer code, there’s a ‘start’ method inside SleepTimer, which

  • 0

In the the following tea timer code, there’s a ‘start’ method inside SleepTimer, which calls ‘notify.’

  def start
    sleep minutes * 60
    notifier.notify("Tea is ready!")
  end

If you look at the code below, you’ll see that there’s a notify method in the class StdioUi as well as a notify method in module UiWithBeep. The start method shown above calls the notify method in module UiWithBeep, which then, via ‘super,’ calls the notify method in class StdioUi. (The effect is that “BEEP!” is heard before “Tea is ready”.) However, I don’t understand why notifier.notify calls the notify method in module UiWithBeep rather than in class StdioUi.
First Question: how does it know to go to the one ‘notify’ over the other.
SecondQuestion And, although I understand super, what establishes the relationship so that notify in class StdioUi is ‘super’ to the other notify. Can you please explain

Tea Timer

class TeaClock
  attr_accessor :timer
  attr_accessor :ui

  def initialize(minutes)

    self.ui = StdioUi.new

    self.timer = SleepTimer.new(minutes, ui)
    init_plugins

  end

  def init_plugins
    puts "init plugins"
    @plugins = []
    ::Plugins.constants.each do |name|
      @plugins << ::Plugins.const_get(name).new(self)
    end
  end

  def start
    timer.start
  end
end

class StdioUi
  def notify(text)
    puts text
  end
end

SleepTimer = Struct.new(:minutes, :notifier) do
  def start
    sleep minutes * 60
    notifier.notify("Tea is ready!")
  end
end

module Plugins
  class Beep    
    def initialize(tea_clock)

      tea_clock.ui.extend(UiWithBeep)
    end

    module UiWithBeep
      def notify(*)         #gets called by notifier.notify("Tea is ready")
        puts "BEEP!"

        super               #calls notify in class StdioUi
      end
    end
  end
end

t = TeaClock.new(0.01).start
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:50:40+00:00Added an answer on June 12, 2026 at 11:50 pm

    The Book: I keep recommending this excellent book, Metaprogramming Ruby. I was consulting it while composing this answer.


    So, here you extend an object with a module. In Ruby it’s called Object Extension. In simple cases it all works as expected, like this one:

    module Foo
      def hello
        puts "foo"
      end
    end
    
    class Bar
    end
    
    bar = Bar.new
    bar.extend Foo
    bar.hello
    # >> foo
    

    Things get complicated when there are class’ own methods involved. Here’s a simplified version of your snippet that exhibits the same behaviour.

    module Foo
      def hello
        puts "foo"
        super
      end
    end
    
    class Bar
      def hello
        puts 'bar'
      end
    end
    
    bar = Bar.new
    bar.extend Foo
    bar.hello
    # >> foo
    # >> bar
    

    When you call a method in ruby, the interpreter has to first find a method to call. This is called Method Lookup. Now, when you define an instance method, in reality it’s a method on class object, not that instance. So, method lookup goes like this for first snippet:

     1) bar instance => method hello not found here
     2) Bar class => method hello found
    

    When you extend an object, however, methods are injected into instance’s eigenclass. It’s a special “hidden” class, unique for each instance. And in reality method lookup goes through it first. First snippet again:

     1) bar instance => method hello not found here
     2) bar's eigenclass => method hello not found here
     3) Bar class => method hello found
    

    Now it should be clear why Foo.hello is called instead of Bar.hello: because it appears earlier in the method lookup process!

     1) bar instance => method hello not found here
     2) bar's eigenclass => method hello found
    

    I may have made a few mistakes but this is roughly what happens.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Following example code from the libpcap documentation yields the following code which should report
From the following code: <ol> <li>Coffee</li> <li>Tea</li> <li>Milk</li> </ol> How do I create an
Interesting one, this. I have the following JS code, which I'm displaying in a
Following is the code which i am using to add events in my android
Following is a sample code from my program which queries the database and results
Following code produces a nested array as a result for keys containing three items:
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following chunk html code works as expected: <iframe src=http://www.amazon.com/></iframe> But when trying embed inner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.