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
class VirtualProxy < BasicObject | |
def initialize(&loader) | |
@loader = loader | |
@object = nil | |
end | |
def method_missing(name, *args, &block) | |
__load__ | |
@object.public_send(name, *args, &block) | |
end |
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 'delegate' | |
#---------------------------------------------------------------------- | |
class Logger | |
DEFAULT_FORMATTER = '%d' | |
def initialize(formatter) | |
@formatter = formatter || DEFAULT_FORMATTER |
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
#---------------------------------------------------------------------- | |
module Util | |
module StringFormatter | |
module_function | |
def prettry_string(string) | |
"-> [#{string}] <-" | |
end | |
end |
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 'forwardable' | |
class Vehicle | |
extend Forwardable | |
def_delegators :@vehicle, :name, :power, :color | |
def to_s | |
"#{name} with #{power} HP" |
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
# singleton object are usefull if you | |
# want an object that is stateless | |
class << (FERRARI = Object.new) | |
def to_s | |
"Ferrari from Maranello" | |
end | |
end | |
case FERRARI | |
when FERRARI then puts FERRARI # "Ferrari from Maranello" |
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
#------------------------------------------------------------------------ | |
def Fallback(lambda, &fallback) | |
fallback ||= -> { raise } | |
begin | |
lambda.call() | |
rescue | |
fallback.call() | |
end | |
end |
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
module Registry | |
class << self | |
def logger | |
# some default logger... | |
end | |
def server | |
# some server | |
end |
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 'benchmark' | |
require 'delegate' | |
#--------------------------------------------------------------- | |
# ruby normal implementation | |
#--------------------------------------------------------------- | |
module Mover | |
def accelerate(speed:) | |
@speed += speed | |
end |
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
class NullObject | |
def method_missing(*) | |
self | |
end | |
def respond_to_method_missing?(name) | |
true | |
end | |
def nil? |
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
class Result | |
def self.success | |
Result.new | |
end | |
def self.error(error) | |
Result.new(error) | |
end |
OlderNewer