Last active
December 27, 2018 13:27
-
-
Save jinhucheung/dcba2ee5fd440990e43786ca5284722e to your computer and use it in GitHub Desktop.
Call method of a Ruby Singleton without instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'singleton' | |
require 'forwardable' | |
class Postman | |
include Singleton | |
class << self | |
extend Forwardable | |
def_delegators :instance, :post, :delete | |
end | |
def post | |
puts 'post!' | |
end | |
def delete | |
puts 'delete!' | |
end | |
end | |
Postman.post #=> 'post!' | |
Postman.delete #=> 'delete!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'singleton' | |
class Postman | |
include Singleton | |
class << self | |
delegate :post, :delete, to: :instance | |
end | |
def post | |
puts 'post!' | |
end | |
def delete | |
puts 'delete!' | |
end | |
end | |
Postman.post #=> 'post!' | |
Postman.delete #=> 'delete!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment