Skip to content

Instantly share code, notes, and snippets.

@karuppasamy
Forked from vonconrad/after_method_missing.rb
Created December 23, 2010 08:54
Show Gist options
  • Select an option

  • Save karuppasamy/752755 to your computer and use it in GitHub Desktop.

Select an option

Save karuppasamy/752755 to your computer and use it in GitHub Desktop.

Revisions

  1. vonconrad revised this gist Oct 7, 2010. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions after_method_missing.rb
    Original file line number Diff line number Diff line change
    @@ -4,13 +4,12 @@ def initialize
    end

    def method_missing(name, *args)
    if name.to_s == "emergency"
    @desk.emergency
    else
    unless name.to_s == "emergency"
    hour = Time.now.hour
    raise "Out for lunch" if hour >= 12 && hour < 14
    @desk.send(name, *args)
    end

    @desk.send(name, *args)
    end
    end

  2. @nusco nusco revised this gist Oct 4, 2010. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions after_method_missing.rb
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,13 @@ def initialize
    @desk = InformationDesk.new
    end

    def method_missing(method)
    if method.to_s == "emergency"
    def method_missing(name, *args)
    if name.to_s == "emergency"
    @desk.emergency
    else
    hour = Time.now.hour
    raise "Out for lunch" if hour >= 12 && hour < 19
    @desk.send(method)
    raise "Out for lunch" if hour >= 12 && hour < 14
    @desk.send(name, *args)
    end
    end
    end
  3. @nusco nusco revised this gist Oct 4, 2010. 1 changed file with 10 additions and 26 deletions.
    36 changes: 10 additions & 26 deletions after_method_missing.rb
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,19 @@
    # This class didn't change...
    class InformationDesk
    def flights
    # Provide flight information
    "flights() called"
    end

    def local_transports
    # Return a metro map
    "local_transports() called"
    end

    def hotels
    # Find a suitable hotel
    "hotels() called"
    end

    # Even more methods...
    end

    # ...but this one sure did!
    class DoNotDisturb
    def initialize
    @desk = InformationDesk.new
    end

    def method_missing(method)
    hour = Time.now.hour
    raise "Out for lunch" if hour == 12 || hour == 13
    @desk.send(method)
    if method.to_s == "emergency"
    @desk.emergency
    else
    hour = Time.now.hour
    raise "Out for lunch" if hour >= 12 && hour < 19
    @desk.send(method)
    end
    end
    end

    # At 10 o'clock in the morning:
    DoNotDisturb.new.flights # => "flights() called"
    # At 12:30...
    DoNotDisturb.new.emergency # => "emergency() called"
    DoNotDisturb.new.flights # ~> -:37:in `method_missing': Out for lunch (RuntimeError)
  4. @nusco nusco created this gist Oct 4, 2010.
    35 changes: 35 additions & 0 deletions after_method_missing.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # This class didn't change...
    class InformationDesk
    def flights
    # Provide flight information
    "flights() called"
    end

    def local_transports
    # Return a metro map
    "local_transports() called"
    end

    def hotels
    # Find a suitable hotel
    "hotels() called"
    end

    # Even more methods...
    end

    # ...but this one sure did!
    class DoNotDisturb
    def initialize
    @desk = InformationDesk.new
    end

    def method_missing(method)
    hour = Time.now.hour
    raise "Out for lunch" if hour == 12 || hour == 13
    @desk.send(method)
    end
    end

    # At 10 o'clock in the morning:
    DoNotDisturb.new.flights # => "flights() called"