What is the best / most used one-time character encoding convention in Ruby?

In the Ruby Standard Library, we have a Singleton class:
http://ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html

We can make any singleton class by including this class inside. I rarely see it being used. When would it be wise to use this Singleton class or just use the simple old class methods - also known as single-user mode methods?

It is said differently: which Singleton coding scheme is the best and why? Here are three ways I could think of:

require 'singleton'
class Foo
  include Singleton
  # method definitions go here...
end

Foo.instance.do_something!

Against

class Foo
  class << self
    # method definitions go here...
  end
end

Foo.do_something!

Against

module Foo
  class << self
    # method definitions go here...
  end
end

Foo.do_something!
+3
source share
2 answers

WARNING : Opinions Ahead!


, :

class << (Foo = Object.new)
  # method definitions go here...
end

Foo.do_something!

. - . : , , factory .

, Ruby, , "Ruby": Ruby, Rails, Ruby, Smalltalk, Ruby, Java- Ruby Ruby? , .

, , , . , mixin extend . (, !)

, , , , , .

+4

, , - , . - .

0

Source: https://habr.com/ru/post/1785092/


All Articles