
Security News
Follow-up and Clarification on Recent Malicious Ruby Gems Campaign
A clarification on our recent research investigating 60 malicious Ruby gems.
Multiton is an implementation of the multiton pattern in pure Ruby. Some its features include:
Add this line to your application's Gemfile:
gem "ruby_multiton"
And then execute:
$ bundle
Or install it yourself as:
$ gem install ruby_multiton
The easiest use case for Multiton is to implement a singleton class. To achieve this Multiton mimics the approach used by the Singleton module from Ruby's standard library:
require "multiton"
class C
extend Multiton
end
C.instance.object_id #=> 47143710911900
C.instance.object_id #=> 47143710911900
As an example lets create a singleton object that returns the amount of seconds elapsed since it was first initialized:
class Timestamp
extend Multiton
def initialize
self.initialized_at = Time.now
end
def elapsed_seconds
Time.now - initialized_at
end
private
attr_accessor :initialized_at
end
Timestamp.instance #=> #<Timestamp:0x00562e486384c8 @initialized_at=2017-04-17 20:00:15 +0000>
# Some time later...
Timestamp.instance.elapsed_seconds #=> 542.955918039
To implement a multiton class we will need a key
to be able to access the different instances afterwards. Multiton
achieves this by using the parameters passed to the initialize
method as the key
:
require "multiton"
class C
extend Multiton
def initialize(key)
end
end
C.instance(:one).object_id #=> 47387777147320
C.instance(:one).object_id #=> 47387777147320
C.instance(:two).object_id #=> 47387776632880
C.instance(:two).object_id #=> 47387776632880
As an example lets create multiton objects representing playing cards that we can check if they were drawn or not:
class Card
extend Multiton
def initialize(number, suit)
self.has_been_drawn = false
end
def draw
self.has_been_drawn = true
end
def drawn?
has_been_drawn
end
private
attr_accessor :has_been_drawn
end
Card.instance(10, :spades).drawn? #=> false
Card.instance(5, :hearts).draw
Card.instance(10, :spades).draw
Card.instance(5, :hearts).drawn? #=> true
Card.instance(10, :spades).drawn? #=> true
Card.instance(2, :diamonds).drawn? #=> false
Multiton instances can be serialized and deserialized like regular objects. Continuing with the previous Card
example:
Card.instance(2, :diamonds).drawn? #=> false
serialized_card = Marshal.dump(Card.instance(2, :diamonds))
Marshal.load(serialized_card).drawn? #=> false
Card.instance(2, :diamonds).draw
Marshal.load(serialized_card).drawn? #=> true
Multiton supports cloning, duplicating and inheriting from a multiton class:
require "multiton"
class C
extend Multiton
end
D = C.clone
E = D.dup
class F < E
end
F.instance.object_id #=> 47418482076880
F.instance.object_id #=> 47418482076880
Note that C
, D
, E
and F
are all considered different classes and will consequently not share any instances.
git checkout -b my-new-feature
)git commit -am "Add some feature"
)git push origin my-new-feature
)FAQs
Unknown package
We found that ruby_multiton demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.
Security News
ESLint now supports parallel linting with a new --concurrency flag, delivering major speed gains and closing a 10-year-old feature request.
Research
/Security News
A malicious Go module posing as an SSH brute forcer exfiltrates stolen credentials to a Telegram bot controlled by a Russian-speaking threat actor.